Blockhead7360
Blockhead7360

Reputation: 135

Run a .command file after giving permissions on macOS in Java

I am trying to generate and execute a .command file in my Java program in macOS.

The program successfully generates the file, but it's the executing that's the issue.

Runtime.getRuntime().exec("chmod a+x \"" + U.base + File.separator + "/Game/run.command\"");
Desktop.getDesktop().open(new File(U.base, "Game/run.command"));

When this is run, it still gives me the popup dialog:

The file "run.command" could not be executed because you do not have appropriate access privileges.

However, when I run the command "chmod a+x filepath" separately in terminal manually, the program is able to execute the file. I assume this is an issue with my usage of "Runtime.getRuntime().exec()"

Is there a way I can fix this?

Upvotes: 1

Views: 1507

Answers (2)

Blockhead7360
Blockhead7360

Reputation: 135

When running the chmod command through the Runtime, it apparently takes the quotes (\") as a part of the path. When I removed the quotes from the ends, it worked.

Upvotes: 0

Guilherme Mussi
Guilherme Mussi

Reputation: 1057

You probably need to set chmod 755 as well.

chmod a+x will add the exec bits to the file but will not touch other bits. For example file might be still unreadable to others and group.

chmod 755 will always make the file with perms 755 no matter what initial permissions were.

If it still doesnt work, it would be good to have an Exception to analyze.

Upvotes: 1

Related Questions