1

I am trying to write a Java program that will execute a line of code on the Windows command prompt. I would like to use an external program (7-Zip) to extract some RAR files (it's more complicated than that but it doesn't matter for the problem at hand).

I got the Runtime instance and used the .exec() method. It works fine when I try to extract one archive by itself but when I try to extract many at the same time, the process gets hung up and .waitFor() never returns.

I researched the problem and believe it is being caused by the output the process is producing. It seems that some buffer or another is filling up and locking up the program. I was outlined here. I implemented this solution (except I used an imput stream and directed it to a file) and it did indeed work. However, it seems to take a lot of extra time to write all that completely unnecessary output.

I was wondering if there was a way to trick the BufferedReader into thinking it has written all that is in its buffer without actually writing it?

Thanks for reading all the way to the bottom!

G-Cam
  • 261
  • 3
  • 12
  • Redirect the output to `NUL`. See this :http://stackoverflow.com/questions/313111/dev-null-in-windows – mgiri935 Oct 03 '13 at 08:56
  • You may be interested in BufferedReader.skip(long n). It allows you to skip a known amount of characters. Not exactly certain of a good way to skip all of the buffer though. – DeadlyFugu Oct 03 '13 at 08:56
  • mgiri935, I did see that post and it does seem to work on some level. However, when I use it, the waitfor() method never actually waits. I think maybe it assumes the process is already done since it has no output or something. – G-Cam Oct 03 '13 at 20:14
  • DeadlyFugu, I tried using the skip method. I used in infinite while loop that kept skipping characters until the amount it could skip was different from the amount that it did. This method worked fine but it was slower that just writing all the output. Thanks though. I did not know of this mehtod – G-Cam Oct 03 '13 at 20:33

1 Answers1

1

It's not the Java part that is hanging, it is the native application which blocks when it cannot write it's output anymore.

Since you already have the code to make it work in Java, why don't you just stick with that?

The next best option would be to make the external process not produce any output. Maybe there is a "silent" flag? On Linux, you can also pipe to /dev/null, not sure if Windows has an equivalent (I suppose you could pipe into a file).

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656