views:

58

answers:

1

Hi *, I'm trying to start a process externally with Java and can't read anything from its InputStream - dunno why!

If I'm starting a process with commands like "ls", "ps" or "kill" everything works fine. I can start the process and get information either on the InputStream or the ErrorStream of the Process.

If I try to use a command like "ftp" or "telnet", which is essential, both InputStream or ErrorStream are blocking my program when trying to read. No information is passed through these streams at any time :-(.

Do anyone know this issue? Is it just not possible with these commands or is anything wrong in my mindset of how to use the Process class with Java?!

pls help me, I'm getting mad on this stuff!

     String processName = _configuration.getProgramCommand().getCommand();
   ProcessBuilder procBuilder = new ProcessBuilder(processName);   

   System.out.println("Starting process "+processName);   
   _proc = Runtime.getRuntime().exec(processName);// procBuilder.start();            

   if(!procBuilder.redirectErrorStream()) {    
    _errorWorker = new ProcessErrorWorker(_proc);
    _errorWorker.start();   
   }

   String proc_start_answer = _configuration.getNextCommand().getCommand();
   System.out.println("Waiting for process answer '"+proc_start_answer+"'");
   BufferedReader input = new BufferedReader(new InputStreamReader(_proc.getInputStream()));      

   String answer = "";  

   try {         
    System.out.println("inputstream ready: "+input.ready());
    answer+=input.readLine(); 
    System.out.println("process answer:  "+answer);
    input.close();        

   } catch(Exception e) {
    System.out.print(e.getMessage());     
   } 
+1  A: 

You need to do this work in a Thread. For example to log the standard output:

Process process = Runtime.getRuntime().exec(command);
LogStreamReader lsr = new LogStreamReader(process.getInputStream());
Thread thread = new Thread(lsr, "LogStreamReader");
thread.start();


public class LogStreamReader implements Runnable {

    private BufferedReader reader;

    public LoggingStreamReader(InputStream is) {
        this.reader = new BufferedReader(new InputStreamReader(is));
    }

    public void run() {
        try {
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Then you need a second thread for input handling. And you might want to deal with stderr just like stdout.

nhnb
Thank you for your answer. Actually I've been testing it within a thread, in all variations. I tried reading whole lines, reading single chars and reading bytes. No effect. I can't get a single bit out of this InputStream, and it should definitely response. It only works if the started process is terminating (like "ps", "ls"). But I also need it to run with "interactive" processes like ftp and telnet, there must be an issue I'm not aware of. any futher ideas?
mgratzer