Java 中 chmod 命令的权限被拒绝错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3085897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permission denied error in Java for chmod command
提问by James Skidmore
I have an executable file (ffmpeg) that I'm trying to run with a Java program on a Mac. I used the Java program to send the command chmod 777 /path/to/ffmpeg
, but when I try to run ffmpeg, I get the following error:
我有一个可执行文件 (ffmpeg),我试图在 Mac 上使用 Java 程序运行它。我使用Java程序发送命令chmod 777 /path/to/ffmpeg
,但是当我尝试运行ffmpeg时,出现以下错误:
java.io.IOException: Cannot run program "/Users/james/WalkTheHall/ffmpeg": error=13, Permission denied
But when I run chmod 777 /path/to/ffmpeg
from Terminal on my own before opening the Java application, the command to ffmpeg will run just fine in the Java program.
但是当我chmod 777 /path/to/ffmpeg
在打开 Java 应用程序之前自己从终端运行时,ffmpeg 的命令将在 Java 程序中运行得很好。
Is there a difference between calling chmod
from within the Java program and calling it on my own? Why will it not work? Thank you!
chmod
从 Java 程序内部调用和我自己调用它之间有区别吗?为什么它不起作用?谢谢!
采纳答案by CarlG
I'd guess that chmod
is a shell command, not an executable. Try running chmod
through your shell. See more details here: Want to invoke a linux shell command from Java
我猜这chmod
是一个shell命令,而不是一个可执行文件。尝试chmod
通过您的外壳运行。在此处查看更多详细信息:想要从 Java 调用 linux shell 命令
回答by Ryan Hayes
Yes, there is a difference. When you run the command from the terminal, it is you who is performing the action, and thus it is performed using your credentials. The Java application is running the command using the Java application's permissions. This is to prevent an application from running and then making dangerous, unwanted changes to the file system. Perhaps someone else can elaborate and give guidance to a workaround for this.
是,有一点不同。当您从终端运行命令时,是您在执行操作,因此它是使用您的凭据执行的。Java 应用程序正在使用 Java 应用程序的权限运行命令。这是为了防止应用程序运行,然后对文件系统进行危险的、不需要的更改。也许其他人可以详细说明并指导解决此问题的方法。
回答by putgeminmouth
I am currently working on a project that also makes use of FFMpeg on OSX. I store FFMpeg in the JAR and extract it and set executable on use as you seem to be doing. This is what I do, and it seems to work.
我目前正在开发一个也在 OSX 上使用 FFMpeg 的项目。我将 FFMpeg 存储在 JAR 中并提取它并设置可执行文件,就像您在做的那样。这就是我所做的,它似乎有效。
public static void setExecutable(File file, boolean executable) { Process p = Runtime.getRuntime().exec(new String[] { "chmod", "u"+(executable?'+':'-')+"x", file.getAbsolutePath(), }); // do stuff to make sure p finishes & capture output }
The code is GPL, so feel free to check it out. Its not the nicest codebase, and even the FFMpeg stuff is perhaps overly complex, but it works.
代码是 GPL,所以请随意查看。它不是最好的代码库,甚至 FFMpeg 的东西也可能过于复杂,但它有效。
Source is viewable at http://korsakow.net
来源可在http://korsakow.net上查看
These two files in particular might be interesting for you
这两个文件可能对您特别感兴趣
回答by macrokigol
I just had the same problem in my code. i solved this by add waitFor after exec. The "chmod" process is not finished when next command is executed. the code may look like:
我只是在我的代码中遇到了同样的问题。我通过在 exec 之后添加 waitFor 解决了这个问题。执行下一个命令时,“chmod”过程并未完成。代码可能如下所示:
p = Runtime.getRuntime.exec("chmod 777 xxx");
p.waitFor();
Runtime.getRuntime.exec("./xxx");
回答by Jefferson Lima
Try this:
尝试这个:
File commandFile = new File("myFile.txt");
commandFile.setExecutable(true);
Process p = Runtime.getRuntime.exec(commandFile.getAbsoluteFile());