运行 在 Java 中授予 macOS 权限后的 .command 文件

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

我正在尝试在我的 Java macOS 程序中生成并执行一个 .command 文件。

程序成功生成文件,但问题在于执行。

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

当这是 运行 时,它仍然给我弹出对话框:

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

然而,当我在终端手动运行命令"chmod a+x filepath"时,程序能够执行该文件。我认为这是我使用 "Runtime.getRuntime().exec()"

的问题

有什么办法可以解决这个问题吗?

您可能还需要设置 chmod 755

chmod a+x 会将 exec 位添加到文件中,但不会触及其他位。例如,othersgroup.

可能仍然无法读取文件

chmod 755 无论初始权限是什么,都会使文件的权限始终为 755。

如果还是不行,有Exception分析一下就好了。

当 运行 chmod 命令通过运行时时,它显然将引号 (\") 作为路径的一部分。当我从末尾删除引号时,它起作用了。