-1

Does anybody know how to execute a shell script from java application? I'm using win 7 to develop java application and the script file is on my hard disk.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
itro
  • 7,006
  • 27
  • 78
  • 121
  • Similar to http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code – alain.janinm Feb 21 '12 at 12:25
  • 1
    What makes you think you can run a UNIX Shell Script on a Windows 7 machine at all? – Erick Robertson Feb 21 '12 at 12:25
  • Application should run on the windows while unix shell script should be execute in the Unix server through this application. – itro Feb 22 '12 at 09:13
  • You want your application to ask your server to run the script, then, it's more about sending a request to your unix server and getting the result (if needed) than about running the code from your java application, it's an entirely different problem. – Tshirtman Feb 22 '12 at 22:23

3 Answers3

3

Hope this will serve your purpose:

import java.io.IOException;
import java.io.InputStream;

public class RunShellScript {

    public static void runShellScript(String unixCommand) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", unixCommand);
        processBuilder.redirectErrorStream(true); 
        Process shellProcess = processBuilder.start();
        InputStream inputStream = shellProcess.getInputStream(); 
        int consoleDisplay;
        while((consoleDisplay=inputStream.read())!=-1) {
            System.out.println(consoleDisplay);
        }
        try {
            inputStream.close();
        } catch (IOException iOException) { }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        String unixCommand = "sh hello-world.sh"; 
        runShellScript(unixCommand);
    }
}

Above code will run the script included in hello-world.sh file and it will display the output on the shell script console.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1218985
  • 7,531
  • 2
  • 25
  • 31
  • This above code seams to me work but I get error while running application. **Error: sh.exe stop working.** what is the reason? – itro Feb 22 '12 at 09:16
1

You would use the exec() family of methods in the java.lang.Runtime class. Of course, you can't execute UNIX shell scripts on your Windows machine without downloading software like MinGW or Cygwin to support that (maybe you mean you're going to execute the script when your program runs on another machine.)

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Can you please provide code snipe how to use MinGW or Cygwin ? – itro Feb 27 '12 at 10:24
  • These are both environments that you install that provide UNIX command-line tools (i.e., `bash`, `ls`, `cat`, `mv`, and hundreds more) on Windows. You can then run them from Java just as you would on UNIX -- see, for example, @SarathKumarSivan's answer. – Ernest Friedman-Hill Feb 27 '12 at 12:07
0

First, to execute a Unix shell script on your Windows 7 system, you'd need a Unix shell. There are several available including cygwin. Assuming you go with bash (most common these days), the command to execute would be bash -c scriptname to execute your script. If you're just executing a Windows cmd or bat file the command is something like cmd /c scriptname You should check the help for cmd to verify this.

Once you start the process, you need to immediately start a thread to begin reading its stdout. You need to get the output stream from the process and begin reading from it. If you don't the pipe between the two processes will fill up and the sub-process will hang. You also need to do the same thing for the child process' stderr unless you use the option to merge the two streams when you create the process.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57