我是java新手。我負責編寫Java程序來運行命令行。我在DOS提示符下測試了命令行,因爲我還沒有訪問Linux的盒子。它工作得很好。有關完整的命令行語法,請參閱下面的PROGRAM。該作業將需要6個輸入文件並生成一些輸出文件。接下來,我嘗試創建一個類並使用getruntime和過程來處理這個工作。即使它編譯沒有錯誤,但是當我運行它只顯示光標閃爍...我想我需要使用線程異步技術。請提供一些建議,因爲我沒有足夠的時間來完成這些項目。我還想在作業完成時實施回調或返回值。一個例子將不勝感激。由於如何在java中使用線程異步運行命令行
import java.io.*;
public class RunJob {
// public static final String PROGRAM = "c:\\wrk\\java.exe Hello";
//one command line below
public static final String PROGRAM = "c:/java.exe -cp \"wrk/jmp.jar;wrk/colt.jar\" gov.lanl.yadas.reliability.UltimateMissileReliabilityModel 10000 \"wrk/\" x1.dat x2c.dat x3.dat x4.dat x5.dat x6.dat true";
// Set to true to end the loop
static boolean done = false;
public static void main(String argv[]) throws IOException {
BufferedReader is;
String line;
String returnMsg = "Start ";
final Process p = Runtime.getRuntime().exec(PROGRAM);
System.out.println("start");
Thread waiter = new Thread() {
public void run() {
try {
p.waitFor();
} catch (InterruptedException ex) {
System.out.println("InterruptedException");
return;
}
System.out.println("Program terminated!");
done = true;
}
};
waiter.start();
is = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (!done && ((line = is.readLine()) != null))
{
System.out.println(line);
returnMsg = returnMsg + line;
}
System.out.println(returnMsg);
System.out.println("End");
return;
}// main
}