从另一个java类编译java文件

时间:2014-02-06 09:11:31

标签: java

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();

    }

}

我正在尝试从另一个Java类(ExecuteShellComand.java)编译Java文件(str.java)。我想要做的是如果“str.java”成功编译然后我想执行“java str”命令,但是如果编译失败则应该打印正确的堆栈跟踪或错误。我将堆栈跟踪或错误存储在output变量中。

但是当我执行此代码时,虽然“str.java”中有一些错误System.out.println(output)没有打印错误。

2 个答案:

答案 0 :(得分:1)

Process类尝试模拟OS进程。这意味着,进程为错误和正常消息保留不同的输出流,并为输入保留一个流。在UNIX中,应该是:

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

在Java中......

  • abstract InputStream getErrorStream()       获取子进程的错误流。
  • abstract InputStream getInputStream()       获取子进程的输入流。
  • abstract OutputStream getOutputStream()

因此,您应该使用getErrorStream()来获取错误..

重构代码:

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();
    }

}

答案 1 :(得分:1)

如果要从命令捕获错误,则应捕获错误流而不是输入流

所以替换

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

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