开发者

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 proc开发者_StackOverflowess is still here.

Help please.


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.


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?


  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.


Please try the following:

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

I hope it will change something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜