我有一個應用程序在我的PC的指定位置調用java測試類。現在路徑是硬編碼的,我通過從命令行執行它來檢查它的工作情況(如果你想看到它:java -cp C:\Users\user\Documents\workspace\test\build\test.jar org.junit.runner.JUnitCore us.test.DynamicWebserviceInvocationTest
),所以我知道該命令工作正常。確定是否從另一個java應用程序執行java執行成功
事情是,當我做Runtime.getRuntime().exec(command)
,如果我嘗試記錄其生成的過程的結果InputStream和ErrorStream,程序就會陷入困境。我試着用exitValue()和waitFor(),但第一次拋出一個不完整的錯誤,第二次也卡住了。奇怪的是,如果我沒有碰到這些(流,或使用函數)的任何東西,程序沒有問題的結局。
所以我的問題是:這是爲什麼呢?下一步是使用給定的參數構建命令,但是如果我看不到結果輸入,我不能完全確定測試是否正在運行。
的代碼,如果你想看到它:
Runtime runtime=Runtime.getRuntime();
logger.debug("Attempting to execute the test {} at path {}",classpath,applicationLocation);
String command="java -cp C:\\Users\\user\\Documents\\workspace\\test\\build\\test.jar org.junit.runner.JUnitCore us.test.DynamicWebserviceInvocationTest";
Process process=runtime.exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
正如一個測試:你能嘗試閱讀無論是在時間和交替流一行他們之間只要進程還活着? – Dawnkeeper
使用本地套接字互相連接並進行通信。 – farukdgn
@Dawnkeeper:我試了一下,它打印出整個東西,並退出罰款。現在我只需要處理輸出。非常感謝! – KatVolkov