lemoos
lemoos

Reputation: 167

external program from java doesn't terminate

When I try to execute an external program from java I use this code below :

Process p;
rn = Runtime.getRuntime();

String[] unzip = new String[2];

unzip[0]="unzip";

unzip[1]=archive ;

public void dezip() throws IOException{

    p = rn.exec(unzip);
    int ret = p.exitValue();

System.out.println("End of unzip method");

But my last System.out is never executed, as if we exit from unzip method. The unzip() call does only the half of the work, only a part of my archive is unzipped. When I use ps -x or htop from command line I see that unzip process is still here.

Help please.

Upvotes: 0

Views: 562

Answers (4)

Jarek Przygódzki
Jarek Przygódzki

Reputation: 4412

  1. Make sure external program doesn't wait for user input
  2. Check if the the executable path is quoted when launching on Windows systems to handle directories with spaces or special characters.

PS. I was using the java.lang.Runtime class but found that the java.lang.ProcessBuilder class is far superior. You can specify current working directory, and most importantly the system environment.

Upvotes: 1

Jagat
Jagat

Reputation: 1402

Check if the unzip command is prompting for something, perhaps a warning if the file already exists and if you want to overwrite it.

Also, is that a backquote I see in the middle of a java program?

Upvotes: 1

M Platvoet
M Platvoet

Reputation: 1654

You probably need to read the InputStream from the process. See the javadoc of Process

Which states:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Upvotes: 2

goroncy
goroncy

Reputation: 2091

Please try the following:

p = rn.exec(unzip);
p.waitFor()

I hope it will change something.

Upvotes: 0

Related Questions