Please, consider the following snippet which is being run from Eclipse.
Even though the external jar file does not exist no Exception is thrown and process is not null. Why is it so?
try {
Process process = Runtime.getRuntime().exec("java -jar NonExisting.jar");
if (process == null)
System.out.println("process = null");
else
System.out.println(process);
} catch (IOException e) {
System.err.println(e);
}
It prints
java.lang.ProcessImpl@1a4d139
If I run it manully from command line then there is an error:
C:\Users\workspace\Project\src>java -jar NonExisting.jar
Error: Unable to access jarfile NonExisting.jar
Process.waitFor() gives you the exit code of the spawned process, and is likely returning a non-zero (i.e. error) value. You should check this value, and collect the
stdout/errat the same time (see here for more info).stderrwill likely report an error.All you’re currently doing is confirming that the process has been invoked. The process then tries/fails to load the jar file, and that’s when it exists and reports an error.