1

I need to compile and run a java class from inside another java class, then input something into the console of the former.
The (console) output of the tested class needs to be redirected to the testing class. I've already tried something similar where the tested file was given a text document instead of console input and had no problems there, which is why I think that the console input must be the problem.

Here's what I've come up with so far:

@testing
import java.io.*;
public class Test {
    public static void main(String [] args) throws Exception{

      Process javac = Runtime.getRuntime().exec("javac ToBeTested.java");
      javac.waitFor();

      Process process = Runtime.getRuntime().exec("java ToBeTested");
      BufferedReader readFromProcess = new BufferedReader(new InputStreamReader(process.getInputStream ()));
      BufferedReader errorFromProcess = new BufferedReader(new InputStreamReader(process.getErrorStream ()));
      BufferedWriter writeToProcess = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
      writeToProcess.write("Foo");
      System.out.println("Foo written");
      writeToProcess.write("FooBar");
      System.out.println("FooBar written");
      System.out.println("Tested Process:" +readFromProcess.readLine());
      writeToProcess.write("Bar");
      System.out.println("Bar written");
      String line;
      for (String line=readFromProcess.readLine(); line != null; line =readFromProcess.readLine()){
              System.out.println("Tested Process:" +line);
}
      process.waitFor();

    }
}

@tested
import java.io.*;
public class ToBeTested {
    public static void main(String[] args) throws IOException{
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("foo");
      System.out.println(input.readLine());
      System.out.println("fooBar");
        }

    }

Right now the output is the following:

Foo written
FooBar written
Tested Process:null
Bar written

Also, if I comment out the line

writeToProcess.write("Bar");  

the programm will get stuck in

process.waitFor();  

,probably because ToBeTested still expects an input of some sort. Does this mean that the write methods work as intended? If so: Why doesn't the output work?

Please note that the ToBeTested class can't be changed (or even accessed for that matter). It would also be nice to get some sort of notification when an input is expected in ToBeTested, as the number and order of inputs required may vary.

4
  • Have you tried flushing the writeToProcess? I could be wrong but I would think you'd need to Commented Dec 8, 2016 at 23:36
  • flushing writeToProcess gives a "The pipe is being closed" error.
    – Pan
    Commented Dec 8, 2016 at 23:45
  • I think that means your Process closed/ is closing, which wouldn't make sense since you wait for it to close. Have you put a print before the waitFor() to make sure it's really the thing blocking? Also, have you tested that the class you're inputting is actually running? This could be done by putting only a long Thread.sleep() in it Commented Dec 8, 2016 at 23:59
  • I've tried Thread.sleep() but it doesn't change the output. Also the flushing is definitely causing the error (with and without the Thread.sleep() added)
    – Pan
    Commented Dec 9, 2016 at 0:03

0

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.