2

Here's the code: it successfully opens a terminal but nothing is displayed on the output

try {
    String command= "/usr/bin/xterm";
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(command);
    BufferedWriter os = 
        new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
    BufferedReader is = 
         new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line = "";

    while ((line = is.readLine()) != null) {
        System.out.print(line);
    }

} catch (Exception io) {    
}

2 Answers 2

3
  1. Don't write empty catch-blocks. It's just wrong and it will cost you many hours of debugging after which you'll feel ... less than perfect.
  2. xterm produces no output by default. It just displays a window. Try starting xterm in a terminal and see which output it produces (in the original terminal, not in the new window!).
  3. Read When Runtime.exec() won't and follow all of its advice.
Sign up to request clarification or add additional context in comments.

3 Comments

point #1 deserves its own separate upvote. Every time I find an empty try/catch in production code I have to suppress the urge to go stabby.
Besides these excellent points, the error stream of the child process needs to be read too
@Hemal: that's explained in the third item and I don't expect this particular item to be relevant to the problem at hand.
1

Have you read When Runtime.exec() won't. If you read the whole article you will avoid and understand many pitfalls of the exec command.

Then you can read up on ProcessBuilder which is a more modern way to invoke other processes.

Ps. Empty catch block swallow exceptions and make it harder to debug.

1 Comment

Unfortunately ProcessBuilder doesn't solve most of the problems mentioned in this (excellent) article.

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.