Cannot read from Runtime.getRuntime().exec().getErrorStream because process terminates

Discussion in 'Java' started by Zsolt Koppany, Oct 21, 2003.

  1. Hi,

    I start with Runtime.getRuntime().exec(). an executable program and I want
    to read from getErrorStream but I cannot because the process terminates
    (exit(1)) immediately. How can I catch the info sent to the error stream?


    Zsolt
    Zsolt Koppany, Oct 21, 2003
    #1
    1. Advertising

  2. Zsolt Koppany

    Paul Lutus Guest

    Zsolt Koppany wrote:

    > Hi,
    >
    > I start with Runtime.getRuntime().exec(). an executable program and I want
    > to read from getErrorStream but I cannot because the process terminates
    > (exit(1)) immediately. How can I catch the info sent to the error stream?


    Start by posting the Java code that tries to do this, plus the example
    native application call that *doesn't* produce the result you are after.

    I ask this because it is a given that the error stream will contain anything
    the process emits. Example:

    ***************************************************

    public class Test {

    static public void main(String[] args) throws Exception
    {
    Process p = Runtime.getRuntime().exec("ls xxxx");
    BufferedReader br = new BufferedReader(new
    InputStreamReader(p.getErrorStream()));
    String line;
    while((line = br.readLine()) != null) {
    System.out.println("Read from error stream: \"" + line + "\"");
    }
    }
    }

    ***************************************************

    Result (Linux):

    Read from error stream: "/bin/ls: xxxx: No such file or directory"

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Oct 21, 2003
    #2
    1. Advertising

  3. Zsolt Koppany

    SaintMagoo

    Joined:
    Mar 5, 2011
    Messages:
    1
    The Problem with External Commands & Runtime.getRuntime()

    Thought I would toss this one out to the general community - It expresses the problem better - exact same code, yet two work, one will not.

    Anyone care to take a stab at why the stream from 'reader3' will never be available?


    Code:
    package Problems;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     *
     * @author profnagy
     */
    public class RunningProblem {
    
        public static class RunningReader implements Runnable {
    
            private BufferedReader reader;
            private String sName;
    
            public RunningReader(InputStream is, String sName) {
                this.reader = new BufferedReader(new InputStreamReader(is));
                this.sName = sName;
            }
    
            public void run() {
                try {
                    String line = reader.readLine();
                    while (line != null) {
                        System.out.println(sName + ": " + line);
                        line = reader.readLine();
                    }
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) {
            ExecutorService pool = Executors.newFixedThreadPool(3);
            try {
                Runtime rt = Runtime.getRuntime();
    
                Process proc1 = rt.exec("ps ax");
                RunningReader reader1 = new RunningReader(proc1.getInputStream(), "reader1");
    
                Process proc2 = rt.exec("ls -l /");
                RunningReader reader2 = new RunningReader(proc2.getInputStream(), "reader2");
    
                Process proc3 = rt.exec("/bin/tar");
                RunningReader reader3 = new RunningReader(proc3.getInputStream(), "reader3");
    
                pool.execute(reader3);            
                pool.execute(reader2);
                pool.execute(reader1);
    
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
            } finally {
                pool.shutdown();
            }
            System.out.println("Launcher.main() Exited.");
        }
    }
    </code>
    SaintMagoo, Mar 5, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page