1

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) {
 }
asked Jun 20, 2013 at 14:18
1
  • 2
    Note: you should at the very least do Thread.currentThread().interrupt() when catching InterruptedException. Commented Jun 20, 2013 at 14:47

2 Answers 2

3

This is normal: two commands mean two processes. You need two ProcessBuilders, and check the return value of the first process before executing the second one.

This syntax:

new ProcessBuilder("javac","Mocha.java","&&","java","Mocha");

does not work. && is a logical shell operator, the javac command does not understand it. Do your processing logic in Java directly instead:

if (p1.waitFor() == 0) // compile succeeded
 // initiate second process
answered Jun 20, 2013 at 14:21
Sign up to request clarification or add additional context in comments.

2 Comments

Got it, nevermind. I thought the waitFor method was not supposed to return zero.
@phileaton well, it returns the exit code of the command invoked by the process (says the Javadoc) ;)
1

The syntax mentioned works with shell, not with java ProcessBuilder.

Option one is to launch shell and execute shell command. Another is to invoke to ProcessBuilder two times. One for javac another for java

answered Jun 20, 2013 at 14:20

4 Comments

What do you mean by launch shell and execute shell command? Is there another way to do this in Java? From everything I saw, ProcessBuilder was the only acceptable (not-deprecated) way to do this?
(But having two processes does make sense too.)
@phileaton He means using the ProcessBuilder to build a shell process instead of a javac process and then executing the javac in the shell as you were trying (but you got confused because you forgot that the shell is itself a process, and not some magical thingy, and the ProcessBuilder is not actually starting a shell process, only the javac process).
You mean like (for example on Windows) having an existing batch file that compiles and executes a program, and then just calling that instead?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.