我有一个应用程序在我的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