简体   繁体   中英

Java blocks after calling runtime.exec()

First some code:

    Runtime runtime = Runtime.getRuntime();
    String args[] = new String[2];
//  args[0] = "/bin/bash";
//  args[1] = "-c";
//  args[2] = "/usr/bin/rpm2cpio "+archiveFile.getCanonicalPath()+" | /bin/cpio -idmv";
    args[0] = "/usr/bin/rpm2cpio";
    args[1] = archiveFile.getCanonicalPath();
    Process rpm2cpioProcess = runtime.exec(args, null, dir);
//  System.out.println("started rpm2cpio");

    String args2[] = new String[3];
    args2[0] = "/bin/cpio";
    args2[1] = "-idmu";
    args2[2] = "--quiet";
    Process cpioProcess = runtime.exec(args2, null, dir);
//  System.out.println("started cpio");

    InputStream fromRpm2cpio = rpm2cpioProcess.getInputStream();
    new ProcessInputStreamer(rpm2cpioProcess.getErrorStream());
    OutputStream fromCpio = cpioProcess.getOutputStream();
    new PipedStreamer(fromRpm2cpio, fromCpio);
    new ProcessInputStreamer(cpioProcess.getErrorStream());
//  System.out.println("pipe created");
    while(cpioProcess!=null && fromRpm2cpio!=null) {
        boolean doSleep = true;
//      System.out.println("waking up");
        if (cpioProcess!=null) {
            try {
                if (cpioProcess.exitValue()==0) {
                    cpioProcess = null;
                    doSleep = false;
                }
            } catch(IllegalThreadStateException e) {
            }
        }
        if (rpm2cpioProcess!=null) {
            try {
                if (rpm2cpioProcess.exitValue()==0) {
                    rpm2cpioProcess = null;
                    doSleep = false;
                }
            } catch(IllegalThreadStateException e) {
            }
        }
        if (doSleep) {
            Thread.sleep(30);
        }
//      System.out.println("still running");
    }

I'm trying to extract the content of an rpm archive. This code works fine after multiple modifications. My first attempt was to execute the next code through Java:

/bin/bash -c '/usr/bin/rpm2cpio <archive-file> | /bin/cpio -idmv'

Which worked fine the first time I ran it (you can see it in the code commented above). The second time I ran the code it got blocked since the extracted files already existed. So I thought maybe it has to do with the piping and thus split the call into two separate processes. This didn't help much either. So I then modified the arguments of the /bin/cpio from '-idmv' to '-idmu --quiet' and now it works. Unfortunately the -u option overwrites existing files 'unconditionally' which is not really needed. My question is why does it block with -idmv and why doesn't it block with -idmu ?

It could be waiting on standard input for some inputs. Redirect your standard input and/or output to /dev/null

我猜想您的ProcessInputStreamer和/或PipedStreamer实现了Runnable或extent Thread,而您没有在任何地方运行它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM