2

I am using Runtime.getRuntime().exec() to run a shell script from java code.

    String[] cmd = {"sh",  "build.sh", "/Path/to my/sh file"};
    try{
        Process proc = Runtime.getRuntime().exec( cmd );
    }
    catch(Exception e){
        System.out.println("Exception is:"+e);
    }

It gives me the following output in console:

sh: Can't open build.sh

Am I following some wrong approach here? Cannot make out why his happens.

EDIT

Based on the comment here, I have modified the String[] cmd = {"sh", "build.sh", "/Path/to my/sh file"}; to String[] cmd = {"sh", "/Path/to my/sh file/build.sh", "/Path/to my/sh file"}; . Now the problem is this script need to be executed from a particular path. When I execute this script from command prompt, I first change the directory to that path and execute it. How should I modify this code?

2
  • 1
    provide full path of build.sh i.e. path relative to the project folder Commented Jun 17, 2013 at 7:44
  • 1
    Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces. Commented Jun 17, 2013 at 7:55

4 Answers 4

4

Use a ProcessBuilder and set the working directory of the process to the directory where your script actually is:

final ProcessBuilder pb = new ProcessBuilder("/bin/sh", "script.sh", "whatever",
    "arguments", "go", "here");
pb.directory(new File("/path/to/directory"));
// redirect stdout, stderr, etc
final Process p = pb.start();

See the ProcessBuilder javadoc. It contains an example of what you can do. Runtime.exec() is passé :p

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! Learn this class, it is what you MUST use when running programs in Java.
1

sh is unable to find the build.sh script. To fix this you can provide the full path to build.sh.

Comments

1

The problem is that the "sh" command is unable to resolving the relative path "build.sh" to an absolute path. The most likely explanation is that "build.sh" is not in the current directory when you launch the command.

Assuming that "/Path/to my/sh file" string is the path to the "build.sh" file, you need to run it like this:

String[] cmd = {"/bin/sh",  "/Path/to my/sh file/build.sh"};
try {
    Process proc = Runtime.getRuntime().exec(cmd);
    ...

(.... or the equivalent using ProcessBuilder)

On the other hand, if the "/Path/to my/sh file" string is supposed to be an argument to the "build.sh" script, then you need to run it like this:

String[] cmd = {"/bin/sh", "/some/dir/build.sh", "/Path/to my/sh file"};
try {
    Process proc = Runtime.getRuntime().exec(cmd);

@fge's answer gives an alternative approach. He is setting the current directory for the child process before it is launched. That is the correct solution for your updated Question.

3 Comments

String[] cmd = {"/bin/sh", "/home/sreekumar/bin/build.sh"}; works. What if I need to execute this from a particular path. Similar to we change directory in terminal first and then execute the script?
@user1400538 - As I said, fge's Answer address your updated Question. Read it.
ok, done and it works, sorry I read the last part of your question later only. thanks.
1

Try this:

 String[] cmd = {"sh build.sh", "/Path/to my/shfile"};

and better to use ProcessBuilder

ProcessBuilder pb = new ProcessBuilder("sh build.sh", "/Path/to my/shfile"); 

1 Comment

This won't help. You are telling the OS to run an executable with the pathname /bin/sh build.sh ... which definitely does NOT exist on any sane UNIX / Linux system.

Your Answer

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.