views:

670

answers:

5

I'm using this code to make my Java program open a (visible) CMD window:

try {
            String line;
            Process p = Runtime.getRuntime().exec("cmd /C start \"Render\" \"" + myPath + "\\punchRender.cmd\"");
             BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
            System.out.println(line);
            jLabel7.setText(line);

            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }

and I've been trying to do the same thing with the OSX terminal, this is where I'm at right now:

  try {
            String line;
            Process p = Runtime.getRuntime().exec("sh " + myPath + "/punchRender.sh");
             BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
            System.out.println(line);
            jLabel7.setText(line);

            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }

So far, no luck :( Any suggestions? The .sh file isn't even running...

A: 

I assume you've checked that the .sh file is executable, haven't you?

dhiller
That wouldn't be necessary.
Matt Kane
+2  A: 

I would just make sure your shell script has the execute bits on and just pass in the shell script file name.

Process p = Runtime.getRuntime().exec(myPath + "/punchRender.sh")

Edit:

I don't know Java specifically if there is anyway to set file permissions for Unix/Linux with it to set the eXecute bit or how to escape quotes. But It would be something like this:

Process chmod = Runtime.getRuntime().exec("chmod u+x \"" + myPath + "/punchRenderer.sh\"")

Daniel A. White
This will only start a bash process, not the Terminal app.
Matt Kane
I am writing the .sh file with the same program, just before it runs... how can I write it with the execute bits?
Alan
+2  A: 

This should work. Not only running the script, but opening a terminal also:

Process p = Runtime.getRuntime().exec("open -a /Applications/Utilities/Terminal.app \"" + myPath + " /punchRender.sh\"");

Koraktor
This won't work. The OP is trying to capture the output of the script. Process p here is the "open" command which produces no output.
Matt Kane
Oh sorry... missed that.
Koraktor
+1  A: 

If you want a new visible Terminal window, you can't run the shell directly. You need to start Terminal and then run a .command file, not a shell script. I'm not sure how hard it would be to connect the stdout of that command to your Java process. You might have to figure out some other way of getting the output into the terminal.

By the way, I tried your code in a class on my own Mac at home, and it ran a .sh file just fine. I was running the java class from the command line. Maybe sh just isn't in your PATH.

Matt Kane
A: 

Can I suggest you capture the standard error as well as the standard output, and dump that. That should give you some idea as to what's going on (it's good practise generally).

You may need to gather standard output and standard error in different threads to avoid blocking issues. See here for a StreamGobbler

Brian Agnew