1

I am using correct practices mentioned in this article:

http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2

I read the Output and Error streams in concurrently. However, when reading the Output stream of the process, it hangs on readLine() after the last line. I have no idea how to work around this. It is the line of the process and it just hangs there.

The command that hangs this is paexec \192.168.1.92 -c -f C:\Windows\ITBBsync0.bat.

Inside the batch file there are several line such as the following: devcon.exe status =USB > C:\Windows\output.txt

When I execute it on command line, process exits with code 0. When I execute it in Java it hangs after reading the last line of output (which is basically the last line of the batch file). I believe the process is not exiting which is why the issue occurs.

import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;

StreamGobbler(InputStream is, String type)
{
    this.is = is;
    this.type = type;
}

public void run()
{
    try
    {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line=null;
        while ((line = br.readLine()) != null){
            System.out.println(type + ">" + line);
            //hangs after reading last line.
        }
        } catch (IOException ioe)
          {
            ioe.printStackTrace();  
          }
}
}

Also the method that uses the StreamGobbler is the following. There is obviously a parent class which executes this method.

public static boolean cmd(String command){
    try{
    Runtime rt = Runtime.getRuntime();
    System.out.println("Execing " + command);
    Process p = rt.exec(command);
    // any error message?
    StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");
    StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");

    errorGobbler.start();
    outputGobbler.start();


    int exitVal = p.waitFor();
    if (exitVal != 0){
        System.out.println("ExitValue: " + exitVal);        
        System.out.println(command + "EXIT CODE:" + exitVal);
        return false;
    }
    return true;
    }catch(IOException ioe){
        ioe.printStackTrace();
        return false;
    }catch(InterruptedException ie){
        ie.printStackTrace();
        return false;
    }
}

I have visited many threads on StackOverflow and as far as I know I am using best practices for this. Please let me know how I can fix this or why this may be happening.

I can't give you exactly the application I am executing because it is a bit complex, but the application performs some network operations and also executes a batch file which in turns contains more operations.

ThePrince
  • 818
  • 2
  • 12
  • 26
  • 4
    good thing: after you are done with reading, close reader/inputStream by calling `close` – nikis Apr 28 '14 at 20:40
  • Are you *sure* the external process is exiting? Can you provide an example of the command you're running that is causing this to hang? – Brian Roach Apr 28 '14 at 20:44
  • 1
    what command you're using to test it? testing with `ipconfig` did not generate any errors – T.G Apr 28 '14 at 20:49
  • paexec \\192.168.1.92 -c -f C:\Windows\ITBBsync0.bat – ThePrince Apr 28 '14 at 21:13
  • I have edited in some details about the command – ThePrince Apr 28 '14 at 21:17
  • @ThePrince The way you're approaching this is correct. You could switch to `ProcessBuilder` (See: http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream but it's basically exactly what you're doing with two threads) but it would appear that for whatever reason the external process isn't exiting and therefore any reads from those streams are going to block forever. The only workaround short of figuring out why that is would be to match on some known output from your external process (also noted in that linked answer). – Brian Roach Apr 28 '14 at 21:27
  • Thank you for the link. I am reviewing it. What seems clear to me is that the process isn't exiting. – ThePrince Apr 28 '14 at 22:48
  • your running a batch file, so do you know which process is not ending? maybe call each command from within java. or echo something from within the batch to know where you are. Does it exit when you run from terminal without Java? Are u in the correct folder/ dir when starting process? can control that by passing to Process Builder and having it as a 2nd param in your cmd function. Some processes might behave differently if started from another dir as they dont find a resource or file where expected. – tgkprog Apr 29 '14 at 07:01

0 Answers0