2011-11-12 6 views
4

コード:のRuntime.exec()は出力4 'ストリート' プロセス実行

for (int i=0; i < NUM_STREETS; i++) { 
     Process process = runtime.exec("java -classpath \\bin trafficcircle.Street 1 2"); 

     InputStream is = process.getInputStream(); 
     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader br = new BufferedReader(isr); 
     String line; 

     while ((line = br.readLine()) != null && !line.isEmpty()) { 
      System.out.println(line); 
      System.out.flush(); 
     } 

     InputStream es = process.getErrorStream(); 
     InputStreamReader esr = new InputStreamReader(es); 
     BufferedReader br2 = new BufferedReader(esr); 

     while ((line = br2.readLine()) != null && !line.isEmpty()) { 
      System.out.println(line); 
      System.out.flush(); 
     } 

     int exitVal = process.waitFor(); 
     System.out.println("Process exitValue: " + exitVal); 
    } 

'ストリート' である:

public class Street { 

/** 
* @param args 
* 0 - Simulation run time 
* 1 - Flow time interval 
*/ 
public static void main(String[] args) { 
    System.out.println(args[0]); 
    System.out.println(args[1]); 
    System.out.flush(); 
} 

}

プリントアウト:

Error: Could not find or load main class trafficcircle.Street 
Process exitValue: 1 
Error: Could not find or load main class trafficcircle.Street 
Process exitValue: 1 
Error: Could not find or load main class trafficcircle.Street 
Process exitValue: 1 
Error: Could not find or load main class trafficcircle.Street 
Process exitValue: 1 

Eclipseプロジェクトの「Street.class」はパッケージ内の\ binにあるtrafficcircle。 Runtime.execが見つからなければ最初に不平を言うだろうと思ったんだけど...これで何ができるの?

+0

System.out.flush()を試したことがありますか?両端で? – leonm

+0

ちょうど編集したように、しかしまだ何もしませんでした。 – Rooster

+1

プロンプトからそのコマンドを実行しようとしましたか?サブプロセスのエラーストリームを読んで、有用なものがそのプロセスに印刷されているかどうかを確認してください。 –

答えて

2

私はあなたが捨てているエラーを取得していると仮定します。お試しくださいProcessBuilder.redirectErrorStream(true);

コマンドを実行しようとすると、シェルで実行されず、コマンドラインに表示されないエラーが発生する可能性があります。私は明示的に使用します。

"java","-classpath","bin","trafficcircle.Street","1","2"` 

エラーメッセージが表示されていることを確認してください。

別のオプションは、相対パスを使用する(ドットで)

"/bin/bash", "-c", "java -classpath bin trafficcircle.Street 1 2" 
+1

あなたはJava 'Runtime#exec(String)'関数は、引数をコマンドトークン化を実行する 'Runtime#exec(String command、String [] envp、File dir)'に渡します。私のアドバイスは、終了コードを確認することです。 –

+1

+1は '-c'で'/bin/bash'を呼び出すことを考えていませんでした。私はそれが好きです:) – Bohemian

+0

@dma_k訂正ありがとう。 –

0

使用./binのようなシェルを使用することです。

関連する問題