I am trying to execute a jar file in Java using ProcessBuilder. Now, Whenever the sub process called by the ProcessBuilder, throws any exception, the ProcessBuilder don't catch the exception and execution keeps on proceeding continuously.
Below is my code:
try {
ProcessBuilder pb = new ProcessBuilder("java", "-jar", CommonConstants.jarFileLocation,
fileEntry.getAbsolutePath(), CommonConstants.commonFileLocation);
Process p = pb.start();
}
catch (Exception e) {
e.printStackTrace();
}
The catch block is suppose to print any exception whenever the sub process throws any. However, it never does. Am i missing something?
Done it for printing in console....
I used the ProcessBuilder inheritIO method which sets the source and destination for sub process standard I/O to be the same as those of the current Java process.
Hence the modified code...
try {
ProcessBuilder pb = new ProcessBuilder("java", "-jar", CommonConstants.jarFileLocation,
fileEntry.getAbsolutePath(), CommonConstants.commonFileLocation).redirectError(Redirect.INHERIT);
Process p = pb.start();
}
catch (Exception e) {
e.printStackTrace();
}
However, need it to be caught by the catch block. Need Help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With