Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processbuilder in Java is not throwing up the sub process exception

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?

like image 548
Shreyansh Patni Avatar asked Apr 22 '26 13:04

Shreyansh Patni


1 Answers

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!

like image 175
Shreyansh Patni Avatar answered Apr 24 '26 04:04

Shreyansh Patni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!