0

in my java program, i am trying to get the InputStream from a process and print it with this piece of code:

try {
        Process p = Runtime.getRuntime().exec("cmd /c start dammage\\4.simulation.cmd");

        //BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        //StringBuffer sb = new StringBuffer();
        //String line;
        //while ((line = br.readLine()) != null) {  
            //sb.append(line).append("\n");
        //}  
        //System.out.println(sb.toString());

        String input = IOUtils.toString(p.getErrorStream());
        System.out.println(input);
    } catch (IOException ex) {
        Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "Something happened");
    }

I tried both ways shown above (commented and uncommented), but none of them prints anything. So i would like to ask what am i doing wrong here?

I appreciate any help.

3 Answers3

0

The buffered reader solution looks fine. You might be looking in the wrong stream. Try getting from both streams.. Like

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

StringBuffer sb = new StringBuffer();
String line;
//Read the output from the command
while ((line = stdInput.readLine()) != null) {
    sb.append(line).append("\n");
}

//read any errors from the attempted command
while ((line = stdError.readLine()) != null) {
    sb.append(line).append("\n");
}
Syam S
  • 8,421
  • 1
  • 26
  • 36
  • Please don't use StringBuffer, it was replaced by StringBuilder ten years ago. – Peter Lawrey May 21 '14 at 18:55
  • @Peter : I think its not replaced. You should still you StringBuffer if you need thread safety. Go for StringBuilder in non-thread execution and for better performance. (synchronized vs non-synchronized) This is my understanding. Correct me if I'm wrong. Since he is using Process I assume he might be executing inside a thread. – Syam S May 21 '14 at 18:58
  • i did try both streams but both printed nothing – user3617202 May 21 '14 at 19:39
0
  1. Are you sure it should print something? Because, the commented code should work just right provided the command executed is returning non-empty input stream. Try replacing the argument of exec to "cmd". And see if it's able to read from the input stream. Do following. On windows machine it should give you welcome message from cmd (the usual welcome message we get after we run start command prompt).

    Process p = Runtime.getRuntime().exec("cmd");

  2. About the uncommented code, How IOUtils work? Does it read from the error stream repeatedly. Because, IMO, it's just one time read and not the repetitive one.

Hope I don't confuse.

celeritas
  • 398
  • 1
  • 4
  • 16
  • i don't know why but this line doesn't open the cmd. I tried with line Process p = Runtime.getRuntime().exec("cmd /c start"); which opened the cmd but didn't print anything either – user3617202 May 21 '14 at 19:37
0

You should add a p.waitFor(); to give the program time to terminate. Also, verify if you really want to read stdout or stderr

This works for me:

Process p = Runtime.getRuntime().exec("cmd /c java -version");
int ret = p.waitFor();
System.out.println("process terminated with return code: " + ret);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null) {
    sb.append(line).append("\n");
}
System.out.println(sb.toString());
leonbloy
  • 73,180
  • 20
  • 142
  • 190