Pawan
Pawan

Reputation: 32321

Java calling bash script : Permission Denied Error

I am trying to call a bash script from a java class .

This is my java program

import java.io.File;

public class RunBuild {
    public static void main(String[] args) {
        File wd = new File("/home/sai/Jan5WS/ATCore/bin/");
        System.out.println("Working Directory: " + wd);
        Process proc = null;
        try {
            proc = Runtime.getRuntime().exec(" . Ram.sh", null, wd);
            System.out.println(proc.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I have got all the permissions for that script , please see below sai@sai-Aspire-4720Z:~/Jan5WS/ATCore/bin$ chmod 7777 Ram.sh

-rwxrwxrwx 1 sai sai    77 Feb  3 20:53 Ram.sh~
-rwxrwxrwx 1 sai sai    79 Feb  3 20:53 Ram.sh
sai@sai-Aspire-4720Z:~/Jan5WS/ATCore/bin$

Its throwing this exception below

Working Directory: /home/sai/Jan5WS/ATCore/bin
java.io.IOException: Cannot run program "." (in directory
"/home/sai/Jan5WS/ATCore/bin"): error=13, Permission denied
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at RunBuild.main(RunBuild.java:12)
Caused by: java.io.IOException: error=13, Permission denied
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 4 more

I am using Ubuntu Please let me know what could be the problem ??

Upvotes: 2

Views: 11497

Answers (2)

Andreas Fester
Andreas Fester

Reputation: 36630

See your error output - you are trying to execute "." which is a directory, not your shell script:

java.io.IOException: Cannot run program "."

Replace the " . " in your exec() call with "./" to indicate the current directory, make sure that your script has the proper shebang line, like #!/bin/bash, and that it is executable (which you already did):

proc = Runtime.getRuntime().exec("./Ram.sh", null, wd);

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409176

While I don't know how the exec function call works in Java, I really doubt it will run a shell and accept shell commands. And the command you want to execute is . which is an internal BASH alias for the source command.

You have to call a shell explicitly instead:

proc = Runtime.getRuntime().exec("/bin/bash -c Ram.sh", null, wd);

Upvotes: 0

Related Questions