Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, system command fails to run

Tags:

java

system

I'm trying to run a system command to extract a tar.bz2 file to a specified directory. This is the code:

ProcessBuilder myProc = new ProcessBuilder("tar", "-xjf", "/path/to/MyTarFile.tar.bz2"); 
myProc.directory(new File("/directory/i/want/results/in/"));
myProc.start();
System.out.println(myProc.command());

It runs without error, however the file is deleted and not extracted anywhere.

Any help would be greatly appreciated.

like image 974
dave Avatar asked Mar 11 '26 13:03

dave


1 Answers

I know Runtime.exec() has a really nasty feature where if you don't manually drain STDOUT/STDERR, it effectively appears to hang. I would hope that ProcessBuilder corrected that deficiency, but this page includes this tidbit:

A word of caution about the examples in this tip. It is possible that the examples will deadlock if the subprocess generates enough output to overflow the system. A more robust solution requires draining the process stdout and stderr in separate threads.

So, make sure you're handling Process.getInputStream() and Process.getErrorStream(), as I recommended in the comments; it could solve the problem outright!

like image 161
BlairHippo Avatar answered Mar 14 '26 02:03

BlairHippo



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!