1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaからJava以外の命令を実行する

Last updated at Posted at 2019-04-21

利用するクラス、メソッド

java.lang.Runtime#exec

結果のハンドリングは、返されるjava.lang.Processを利用して実施。

コード例

Process proc = Runtime.getRuntime().exec( "java --version" );

実行例

jshell> Process proc = Runtime.getRuntime().exec( "java --version" );
proc ==> Process[pid=19916, exitValue="not exited"]

jshell> proc.exitValue();
$2 ==> 0

入出力

メソッド名がProcess#execを実行した側からの視点になっていることに注意。

実行プロセスからの出力

  • Process#getInputStream

     → Process#execで実行したプロセスが標準出力に出力する内容を取得するためのInputStreamを取得


    <ソースコード例>
import java.io.*;

Process proc = Runtime.getRuntime().exec( "java Hello" );
String line = null;

try ( var buf = new BufferedReader( new InputStreamReader( proc.getInputStream() ) ) ) {
  while( ( line = buf.readLine() ) != null ) System.out.println( line );
}

# Hello.javaの内容
public class Hello {
    public static void main( String[] args ) {
        System.out.println( "Hello, world!!" );
    }
}

<結果>

Hello, world!!
  • Process#getErrorStream

     → Process#execで実行したプロセスが標準エラー出力に出力する内容を取得するための

      InputStreamを取得

<ソースコード例>
※NotExist.javaは存在していないことが前提。


import java.io.*;

Process proc = Runtime.getRuntime().exec( "javac NotExist.java" );
String line = null;

try ( var buf = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ) {
  while( ( line = buf.readLine() ) != null ) System.out.println( line );
}

<結果>

エラー: ファイルが見つかりません: NotExist.java
使用方法: javac <options> <source files>
使用可能なオプションのリストについては、--helpを使用します

実行プロセスへの入力

  • Process#getOutputStream

     → Process#execで実行したプロセスの標準入力へ情報を渡すためのOutputStreamを取得

    <ソースコード例>

import java.io.*;

Process proc = Runtime.getRuntime().exec( "java -cp . SampleCode" );

try ( var buf = new BufferedWriter( new OutputStreamWriter( proc.getOutputStream() ) ) ) {
     buf.write( "3" );
     buf.newLine();
     buf.write( "5" );
     buf.newLine();
}

// 取得したプロセスからの出力内容を表示
try ( var buf = new BufferedReader( new InputStreamReader( proc.getInputStream() ) ) ) {
   while( ( line = buf.readLine() ) != null ) System.out.println( line );
}

// SampleCode.javaの内容
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SampleCode {
	public static void main( String[] args ) throws Exception{
		try ( var buf = new BufferedReader(
				new InputStreamReader( System.in )
				) ) {
			int x = Integer.parseInt( buf.readLine() );
			int y = Integer.parseInt( buf.readLine() );

			System.out.println( x + "*" + y + "=" + x*y );
		}
	}
}

<結果>

3*5=15

その他

  • プロセスの完了を待機する場合は、Process#waitForを実行。
  • プロセスの終了コードは、Process#exitValueで取得可能。

    ただし、プロセスが終了していない状態でexitValueを呼ぶと例外(java.lang.IllegalThreadStateException)が発生する。(下記参照)
Exception in thread "main" java.lang.IllegalThreadStateException: process has not exited
  at java.base/java.lang.ProcessImpl.exitValue(ProcessImpl.java:478)
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?