4

I know there are many threads about this, but none of them worked for me. Here is what I am trying to do:

Javac and run a file from my java code. It works for Windows but I would like to make it also work on UNIX. Here the code:

if(os.equals("win")){
 //For Windows
 try {
 Runtime.getRuntime().exec(
 "cmd /c start cmd.exe /K "
 + "\"cd " + path + "&& "
 + "javac " + name + ".java && "
 + "echo ^>^>" + name + ".java " + "outputs: &&"
 + "echo. &&"
 + "java " + name + " && "
 + "echo. &&"
 + "pause && "
 + "exit\"");
 } catch (IOException e) {
 System.out.println("Didn't work");
 }
 }else{
 try {
 //TODO make it work for UNIX
 Runtime.getRuntime().exec("");
 } catch (IOException e) {
 System.out.println("Didn't work");
 }
 }

The problem is, that on UNIX systems it acts "unpredictable" For exemple:

Runtime.getRuntime().exec("open image.png");

Opens the image but

Runtime.getRuntime().exec("javac Test.java");
Runtime.getRuntime().exec("echo 'hello'");

It does nothing. No Massage.

I am grateful for any input.

UPDATE------------------------------------------------------------

It seems like in contrast to Windows CMD, Terminal needs a InputStreamReader to display what happened in exec. I found this code in another thread and now at least I get some output from the exec Terminal.

 String line;
 Process p = Runtime.getRuntime().exec( "echo HelloWorld2" );
 BufferedReader in = new BufferedReader(
 new InputStreamReader(p.getInputStream()) );
 while ((line = in.readLine()) != null) {
 System.out.println(line);
 }
 in.close();

But things still remain misterious, because executing

Process p = Runtime.getRuntime().exec("javac Test.java");

works and generates a Test.class file. But

Process p = Runtime.getRuntime().exec("javac Test.java && java Test");

does nothing.(Nothing happens. Terminal "executes" without error massages.) Typing this manually in Terminal builds and runs as expected. What am I missing?

asked Oct 20, 2014 at 14:57
7
  • In what way does it not work? What is the output you're getting? What are you expecting? Commented Oct 20, 2014 at 15:02
  • What does it mean "won't work?" Can you share the exception stack trace with us? Commented Oct 20, 2014 at 15:02
  • Won't work = It just doesn't execute the command. From the example Runtime.getRuntime().exec("javac Test.java"); it does not generate a Test.class. Commented Oct 20, 2014 at 15:05
  • Runtime.exec() returns a Process object which has the stdout and stderr streams available - reading these should give you any OS generated error messages. Commented Oct 20, 2014 at 15:08
  • Ok. Interesting. Because on windows all error messages directly got displayed in the cmd window. Commented Oct 20, 2014 at 15:13

1 Answer 1

2

I don't have a Linux system here to play with, but I'm pretty certain that the second command above is trying to compile Java files named "Test.java", "&&", "java" and "Test"; in other words the Runtime.exec() method treats arguments literally instead of applying shell interpretation.

You could probably get it to work with something along the lines of:

Process p = Runtime.getRuntime().exec(
 new String[] { "sh", "-c", "javac Test.java && java Test" });
answered Oct 20, 2014 at 22:01
Sign up to request clarification or add additional context in comments.

2 Comments

Almost! Things work! I can string my commands together now. But one more thing. Test.java has a System.out.println("worked!"); But that does not display anywhere.. (In windows every I/O, even if it is from the just compiled and executed file, get displayed in the same cmd window.) Here the I/O from the compiled file(Test.class) do not show up.
That output should be going into the stdout stream you get from the Process object returned by the exec() call.

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.