Javaコードで「java -version」という基本的なコマンドを実行しようとしています。私のコードは、「ls」、「echo $PATH」などのシェルコマンドで機能します。私のコード:
import java.io.*;
public class cmd {
public static void main(String[] args) {
String command = "java -version";
Process proc = null;
try {
proc = Runtime.getRuntime().exec(command);
} catch (IOException e1) {
System.out.println( "IOException 1" );
}
BufferedInputStream buffer = new BufferedInputStream( proc.getInputStream() );
BufferedReader commandOutput= new BufferedReader( new InputStreamReader( buffer ) );
String line = null;
try {
while ( ( line = commandOutput.readLine() ) != null ) {
System.out.println( line );
}
commandOutput.close();
} catch ( IOException e) {
System.out.println( "IOException 2" );
}
}
}
コードは例外やエラーなしで機能しますが、出力はありません。
String command = "ls" の場合、コードは機能し、出力はコンソールに表示されます。