2

I need to (compile and) run another class with main method in Java and read its output and later write into its input, but focus on reading first. Let's say i need to test wether the application writes "123" on standart output, here's how i magine it:

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) {

        }

    }
}

I loaded the Runtime class created process ith exec() method but the reader doesn't read any data and throws null pointer exception (on method readLine()) instead. I have both classes in different package and i am using netbeans, if it anyhow matters. Any help apriciated.

kajacx
  • 12,361
  • 5
  • 43
  • 70

0 Answers0