Here is the code. It is taking SampleProg.java as input. I am unable to provide the input for SampleProg.java.
What I am looking for is to compile, run and give input for test cases for the program.
I tried using Buffered Writer but I am unable to write into the input program.
It is getting stuck while giving input.
How can i provide the input. I am getting stuck at the part where i am giving input and it is not processing further. I also tried to use ProcessBuilder, but i was stuck with the same issue.
CODE: Example.java
public class Example {
public static void main(String[] args) {
try{
String path = "src/main/java/SampleProg.java";
compile(pathJava);
Process runProcess = run();
runProcess.waitFor();
BufferedReader in = new BufferedReader(new
InputStreamReader(runProcess.getInputStream()));
String s;
Scanner sc = new Scanner(System.in);
while((s = in.readLine()) != null){
System.out.println(s);
op.write(sc.nextInt());
op.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
private static void compile(String pathJava) throws
IOException, InterruptedException {
String [] cmd = new String[4];
cmd[0] = "javac";
cmd[1] = "-d";
cmd[2] = ".";
cmd[3] = pathJava;
Runtime r = Runtime.getRuntime();
Process compileProcess = r.exec(cmd);
compileProcess.waitFor();
}
private static Process run() throws IOException {
String [] cmd = new String[2];
cmd[0] = "java";
cmd[1] = "SampleProg";
Runtime r = Runtime.getRuntime();
return r.exec(cmd);
}
}