I'm trying to write a program that compiles another java file from the command prompt. I'm having an issue with it however. At this point, it is successfully executing the first part where it compiles Mocha.java. However, I want it to also execute that file and display what it outputs. It displays nothing. Any suggestions?
pb = new ProcessBuilder("javac","Mocha.java");
try {
Process shell = pb.start();
OutputStream shellOut = shell.getOutputStream();
shellOut.write("java Mocha".getBytes());
shellOut.close();
InputStream shellIn = shell.getInputStream();
String response = IOUtils.toString(shellIn, "UTF-8");
System.out.println(response);
shellIn.close();
shell.destroy();
} catch (IOException ex) {
System.out.println("failed");
}
Note:
I also tried to have all arguments initally like so:
pb = new ProcessBuilder("javac","Mocha.java","&&","java","Mocha");
But not only did this not work, it did not even compile Mocha.java as it did above.
Thanks!
EDIT:
So I changed this to make two processes. Works great now guys! For anyone interested:
pb = new ProcessBuilder("javac","Mocha.java");
try {
Process shell = pb.start();
int error = shell.waitFor();
shell.destroy();
if (error == 0)
{
pb = new ProcessBuilder("java","Mocha");
shell = pb.start();
InputStream shellIn = shell.getInputStream();
String response = IOUtils.toString(shellIn, "UTF-8");
System.out.println(response);
shellIn.close();
shell.destroy();
}
} catch (IOException ex) {
System.out.println("failed");
} catch (InterruptedException ex) {
}