Java运行另一个类并控制其I / O.

时间:2013-04-02 14:30:54

标签: java process stream runtime exec

我需要(编译并)在Java中使用main方法运行另一个类并读取其输出,然后写入其输入,但首先关注读取。假设我需要测试应用程序在标准输出上写“123”,这是我如何调整它:

public class TestMe {
    public static void main(String[] args) {
        System.out.println("123");
    }
}

...

public class Tester {

    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process exec = runtime.exec("java works.TestMe");
            BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
            if (br.readLine().equals("123")) {
                System.out.println("Your program is working fine");
            } else {
                System.out.println("Yout program is bad and you should feel bad");
            }
        } catch (IOException ex) {

        }

    }
}

我加载了运行时类创建的进程ith exec()方法,但读取器不读取任何数据并抛出空指针异常(在方法readLine()上)。 我有两个类在不同的包中,我使用netbeans,如果它无论如何重要。 任何帮助apriciated。

0 个答案:

没有答案