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

Z

Zsolt Koppany

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
 
P

Paul Lutus

Zsolt said:
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"
 
Joined
Mar 5, 2011
Messages
1
Reaction score
0
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top