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?

Haeri
  • 621
  • 1
  • 12
  • 27
  • In what way does it not work? What is the output you're getting? What are you expecting? – Reinstate Monica -- notmaynard Oct 20 '14 at 15:02
  • What does it mean "won't work?" Can you share the exception stack trace with us? – AlexR Oct 20 '14 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. – Haeri Oct 20 '14 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. – BarrySW19 Oct 20 '14 at 15:08
  • Ok. Interesting. Because on windows all error messages directly got displayed in the cmd window. – Haeri Oct 20 '14 at 15:13
  • PS: Whats up with that down-vote.. Which part shows no research effort; is not clear or not useful?? – Haeri Oct 20 '14 at 15:18
  • Ok I updated OP. @BarrySW19 Thank you for the hint. It got me a step forward. But still I could use some more hints ;) – Haeri Oct 20 '14 at 21:46

1 Answers1

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" });
BarrySW19
  • 3,759
  • 12
  • 26
  • 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. – Haeri Oct 20 '14 at 22:22
  • That output should be going into the stdout stream you get from the Process object returned by the exec() call. – BarrySW19 Oct 20 '14 at 22:27