0
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

    public static void main(String[] args) {

        ExecuteShellComand obj = new ExecuteShellComand();
        String className = "str.java";
        String command = "javac " + className;
        String output = obj.executeCommand(command);
        System.out.println(output);// prints the output of the executed command
    }

    private String executeCommand(String command) {
        StringBuffer output = new StringBuffer();
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }

}

I am trying to compile a Java file (str.java) from another Java class(ExecuteShellComand.java). What I am trying to do is if "str.java" compiles successfully then I want to execute "java str" command, but if the compilation fails then proper stacktrace or errors should be printed. I am storing the stacktrace or the errors in output variable.

But when I execute this code although "str.java" has somes errors in it System.out.println(output) is not printing the errors.

1
  • By the way, since Java 6, java provide a standardized way to compile classes at runtime. Check this article here: java-bytes.blogspot.de/2012/03/… Commented Feb 6, 2014 at 9:56

2 Answers 2

1

If you want to capture the errors from a command then you shall capture error stream instead of Input stream

So replace

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

with

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
Sign up to request clarification or add additional context in comments.

2 Comments

what if i have to capture both the streams ...then ?
Then you can capture both inputsream and errorstream as different bufferedreaders and get data out of them.
1

The Process class tries to mimetize OS process. It means, process keep different output stream for error and normal messages and one stream for input. In UNIX, should be:

wc < file > wc.count 2> wc.error

In Java...

  • abstract InputStream getErrorStream() Gets the error stream of the subprocess.
  • abstract InputStream getInputStream() Gets the input stream of the subprocess.
  • abstract OutputStream getOutputStream()

So, you should use getErrorStream() to get errors..

Refactoring your code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ExecuteShellComand {

    public static void main(String[] args) {
        ExecuteShellComand obj = new ExecuteShellComand();
        String className = "str.java";
        String command = "javac " + className;
        obj.executeCommand(command);
        System.out.println(obj.output);
        System.out.println(obj.errors);
    }

    private String errors;
    private String output;

    private void executeCommand(String command) {
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            errors = readStream(p.getErrorStream());
            output = readStream(p.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String readStream(InputStream inputStream) throws IOException {
        StringBuffer output = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        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.