I call a class which is located somewhere in a jar file (using java -classpath path/file.jar classname) within my java code.
My problem is when the command genKOSCommand
is invalid the call to input.readLine()
will block the program. So I added input.ready()
to hope avoiding blocking. When I debug the program it's ok. Seem to work. But when don't run it in debug the buffer is never ready.
// Execute a command with an argument that contains a space
String[] genKOSCommand = new String[] {
"java",
"-classpath",
Config.XDSI_TEST_KIT_HOME + "/xdsitest/lib/xdsitest.jar;"
+ Config.XDSI_TEST_KIT_HOME + "/xdsitest/classes",
"ca.etsmtl.ihe.xdsitest.docsource.SimplePublisher", "-k",
"C:/Softmedical/Viewer_Test/xdsi-testkit-2.0.4/xdsihome/usr/data/image14.dcm" };
Process child = Runtime.getRuntime().exec(genKOSCommand);
Buffere开发者_如何学编程dReader input = new BufferedReader(new InputStreamReader(
child.getInputStream()), 13107200);
String line = null;
if (input.ready()) {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
try {
child.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Any advice on how to detect problems with the executed command?
Thank you.
You need to wait in a loop for the BufferedReader to be ready.
while (input.ready() == false) { /* intentional empty space here */ }
while ((line = input.readLine()) != null) {
System.out.println(line);
}
/* rest of code follows */
精彩评论