2

I got a code which is running and displaying output successfully if I am executing something like "dir" but not displaying the output if I am running "java -version" or other command from java. Please help:

public static void execJob(){

    try{        

    ProcessBuilder pb = new ProcessBuilder("C:\\myPrograms\\jdk1.7.0_79\\bin\\java.exe", "-version");
    pb.directory(new File("src"));
    Process process = pb.start();
    IOThreadHandler outputHandler = new IOThreadHandler(process.getInputStream());
    outputHandler.start();
    process.waitFor();

    System.out.println(outputHandler.getOutput());
    }catch(Exception e) {
        System.out.println(e.toString());
    }

}

private static class IOThreadHandler extends Thread {
    private InputStream inputStream;
    private StringBuilder output = new StringBuilder();

    IOThreadHandler(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public void run() {
        Scanner br = null;
        try {
            br = new Scanner(new InputStreamReader(inputStream));
            String line = null;
            while (br.hasNextLine()) {
                line = br.nextLine();
                output.append(line + System.getProperty("line.separator"));
            }
        } finally {
            br.close();
        }
    }
3
  • 1
    First do e.printStacktrace instead of e.toString Commented Oct 7, 2015 at 13:00
  • 1
    Try ProcessBuilder.inheritIO(), check that it's writing to stdout and not stderr. Commented Oct 7, 2015 at 13:02
  • Thanks Elliott but inheritIO() didn't help. Any other suggestions.. Commented Oct 7, 2015 at 13:06

2 Answers 2

4

java -version writes to stderr, so you need pb.redirectErrorStream(true); to capture the output.

 ProcessBuilder pb = new ProcessBuilder("C:\\myPrograms\\jdk1.7.0_79\\bin\\java.exe", "-version");
 pb.redirectErrorStream(true);
 ...
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are right, it is writing now. Actually I am trying to do run java programs from command line and display the output live and it is not displaying the output which is very important. why calling some jar and displaying output not working even if java version is working now. Any idea
-1
private static class IOThreadHandler extends Thread {
    private InputStream inputStream;
    private StringBuilder output = new StringBuilder();
    IOThreadHandler(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public void run() {
        try (Scanner br = new Scanner(new InputStreamReader(inputStream))) {
            String line = null;
            while (br.hasNextLine()) {
                line = br.nextLine();
                output.append(line).append(System.getProperty("line.separator"));
            }
        }
    }

    public String getOutput() {
        return output.toString();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.