tags:

views:

12396

answers:

6

It is quite simple to run a Unix command from java.

Runtime.getRuntime().exec(myCommand);

But is it possible to run a Unix shell script from java code? If yes, would it be a good practice to run a shell script from within java code?

+2  A: 

I think you have answered your own question with

Runtime.getRuntime().exec(myShellScript);

As to whether it is good practice... what are you trying to do with a shell script that you cannot do with Java?

Chris Ballance
+7  A: 

I would say that it is not in the spirit of Java to run a shell script from Java. Java is meant to be cross platform, and running a shell script would limit its use to just UNIX.

With that said, it's definitely possible to run a shell script from within Java. You'd use exactly the same syntax your listed (I haven't tried it myself, but try executing the shell script directly, and if that doesn't work, execute the shell itself, passing the script in as a command line parameter).

Jack Leow
Yes, but in many ways that "spirit of Java" or "write once run everywhere" mantra is a myth anyway.
BobbyShaftoe
What's mythical about it?
Chris Ballance
A: 

It is possible, just exec it as any other program. Just make sure your script has the proper #! (she-bang) line as the first line of the script, and make sure there are execute permissions on the file.

For example, if it is a bash script put #!/bin/bash at the top of the script, also chmod +x .

Also as for if it's good practice, no it's not, especially for Java, but if it saves you a lot of time porting a large script over, and you're not getting paid extra to do it ;) save your time, exec the script, and put the porting to Java on your long-term todo list.

Kekoa
+9  A: 

You should really look at Process Builder. It is really built for this kind of thing.

ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory("myDir");
 Process p = pb.start();
Milhous
A: 

I tried to run a shell script from java code, but I am facing problem. The script is in batchstart.sh file -

#!/bin/ksh
export DISPLAY=:0.0

Now the script is run with a dot on the command line -- . batchstart.sh

How do I run it from java? My java code is below. It throws the following exception -

java.io.IOException: .: not found
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:102)
        at java.lang.ProcessImpl.start(ProcessImpl.java:65)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
        at java.lang.Runtime.exec(Runtime.java:591)
        at java.lang.Runtime.exec(Runtime.java:429)
        at SetDisplay.main(SetDisplay.java:12)


import java.io.*;

public class SetDisplay {

   public static void main(String[] args) {

       File wd = new File("/myhomedir/");
       System.out.println("Working Directory: " +wd);
       Process proc = null;

       try {
           proc = Runtime.getRuntime().exec(". batchstart.sh", null, wd);
       } catch (Exception e) {
         e.printStackTrace();
         }
   }
}

How do I make the shell script run ?

Vicky
It looks like it doesn't understand what you mean by ".": try changing it to "/bin/ksh batchstart.sh". Or even better, supply the full path to batchstart.sh.
mando
You should have proc = Runtime.getRuntime().exec("/bin/ksh /path/to/my/batchstart.sh", null, wd);
Milhous
A: 

Just the same thing that solaris 5.10 it works like this "./batchstart.sh" there is a trick I don´t know if your OS accept it use "\. batchstart.sh" instead. this double slash may help.

Saul