0

I am unable to call a shell script from my java code. Here is my code. Can anyone let me know what I am missing here.

protected boolean shellExecute() throws InterruptedException {
    try {
        Process p = Runtime.getRuntime().exec("/home/baibhav/try_scripts/javacall.sh");
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
2
  • Welcome to StackOverflow. Please tell us exactly what is not working. Is there any Exception? Commented Jun 19, 2013 at 13:50
  • Paste in stack trace. Commented Jun 19, 2013 at 13:58

3 Answers 3

0

Try using sh before the script name

Process p = Runtime.getRuntime().exec("sh /home/baibhav/try_scripts/javacall.sh");
Sign up to request clarification or add additional context in comments.

Comments

0

If you are getting IO execption, please check your scripts path

Comments

0

If the script has output, you need to get the returned Process' output and read from it the process does not block.

Process p = Runtime.getRuntime().exec("/home/baibhav/try_scripts/javacall.sh");
InputStream scriptStdout = p.getInputStream();
// Read from input stream and proecess it.

// In finally block:
scriptStdout.close();

You didn't say whether you had an exception thrown or whether the system just hanged. If it hanged, that's probably the reason.

Also, consider using ProcessBuilder instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.