Search code examples
javacommandprocessbuilderchmodcd

Java - Permission Denied when attempting to execute shell script


I am creating a Java program that will execute a JAR file to deploy a database locally on my machine. In order to do that, I need to execute the JAR file from a shell script (this needs to be automated, not manually deployed).

"java -jar some-jar-SNAPSHOT.jar"

The problem is, I get error=13, permission denied, whenever I try to access a shell script of any kind. I have used sudo chmod 777 to make my directories read, write, execute for all users, as well as the JAR file itself. The problem occurs even when I attempt to change directories.

Here's a snippet of the code that I'm using to execute the command:

String[] jarCommand = {"java -jar some-jar-SNAPSHOT.jar"}; Process process = Runtime.getRuntime().exec(jarCommand); System.out.println("Jar File accessed");

Edit: This permissions issue occurs when I attempt to do a simple command, such as:

String[] cd = {jarDirectory.getAbsolutePath(), "cd"}; Runtime.getRuntime().exec(cd);

This is after setting the permissions of the file via chmod.

I've looked at a ton of different, similar questions, and none of the solutions have solved my issue. Any help is greatly appreciated.


Solution

  • This is not about execute permissions on the JAR file. In fact, the JAR file only needs to be readable.

    If your question's title is accurate, this about whether the Java program is able to read and execute the shell scripts that you are talking about:

    • The shell scripts themselves must be executable by the effective user that is attempting to run them. For example, the scripts themselves need to have the relevant execute bit(s) set.

    • The paths to the shell scripts need to be readable (by the effective user ...) all the way from the root directory. (Or the current directory if you are using a dot-relative relative pathname. Or the directories $PATH ... if you are using a simple command name.)


    The odd thing is that the snippet of code is NOT an attempt to run a shell script at all. That is a simple java command. No shell scripting is involved.

    If that is failing with a "permission denied" error, then either the java command is not executable, or the JAR file is not readable.

    I think you need to provide us more information; e.g. all of the relevant output, the real code and the real file / directory names and permissions.


    My program starts by simply attempting to navigate (via CD) to the directory that contains the JAR file itself:

    I assume that you realize that your attempt:

        String[] cd = {jarDirectory.getAbsolutePath(), "cd"};
        Runtime.getRuntime().exec(cd);
    

    does not work. It changes the current directory of the child process, but that has not effect on the parent or on the next child process that you run. You should be using ProcessBuilder and specifically its directory(File) which can be used to choose a different current directory for the child process.

    That means you are probably in the wrong directory, which could lead to a "permission denied" problem.