尽管 chmod 777,java.io.FileNotFoundException(权限被拒绝)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21008976/
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
java.io.FileNotFoundException (permission denied) despite chmod 777
提问by Athlan
I have faced strange poblem while writing Grails application deployed on Tomcat.
我在编写部署在 Tomcat 上的 Grails 应用程序时遇到了奇怪的问题。
After creating simple test controller I want to write test contents in package com
创建简单的测试控制器后,我想在包 com 中编写测试内容
package com.domain.controller
import java.io.File;
import java.io.PrintWriter;
class TestController {
def index() {
// test
try {
PrintWriter writer = new PrintWriter("/home/user/domains/domain.com/public_html/the-file-name.txt");
writer.println("The first line");
writer.println("The second line");
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
I get an exception:
我得到一个例外:
Class java.io.FileNotFoundException Message /home/user/domains/domain.com/public_html/the-file-name.txt (Brak dost?pu)
类 java.io.FileNotFoundException 消息 /home/user/domains/domain.com/public_html/the-file-name.txt (Brak dost?pu)
I have set the chmod to 777 into /home/user/domains/domain.com/public_html/
. And tomcat7.tomcat7
is owner. I have also tried to create this file with the access rights 777 and ownership set to tomcat7, but I still get an exception:
我已将 chmod 设置为 777 到/home/user/domains/domain.com/public_html/
. 并且tomcat7.tomcat7
是所有者。我还尝试使用访问权限 777 和所有权设置为 tomcat7 创建此文件,但我仍然遇到异常:
ls -al /home/user/domains/domain.com/public_html
razem 16
drwxrwxrwx 3 tomcat7 tomcat7 4096 01-08 23:25 .
drwxr-xr-x 8 user user 4096 12-16 17:14 ..
-rwxrwxrwx 1 tomcat7 tomcat7 0 01-08 23:25 the-file-name.txt
What conditions in OS should I also meet?
我还应该满足 OS 中的哪些条件?
I would be very gratefull if someone could clarify the problem.
如果有人能澄清这个问题,我将不胜感激。
EDIT:
编辑:
I have created the directory under /path1
, set 777. The files are stored perfectly.
I have also crated the directory under under /path2/testdir
, but path2 has no permission 777 and chown. It also works. I have also testes the testdir
with characters .
and _
, also works.
我在 下创建了目录/path1
,设置为 777。文件保存完好。我也在 下创建了目录/path2/testdir
,但是 path2 没有权限 777 和 chown。它也有效。我还测试了testdir
字符.
和_
, 也有效。
I am very investigative and cannot understand the behaviour.
我非常善于调查,无法理解这种行为。
采纳答案by JustinKSU
Ensure you that you have read and execute access to all parent directories as well.
确保您还拥有对所有父目录的读取和执行访问权限。
Example:
chmod o+x /home/user
例子:
chmod o+x /home/user
回答by user987339
FileNotFoundExceptionoccurs:
when a file with the specified pathname does not exist. It will also be thrown if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.
当具有指定路径名的文件不存在时。如果文件确实存在但由于某种原因无法访问,例如尝试打开只读文件进行写入时,也会抛出该文件。
File is there and it has write rights according to what you sent.
文件在那里,并且根据您发送的内容具有写权限。
In some cases, if the file that you are trying to access for read/write operation is opened by another program then this error will occur. Use lsof | grep the-file-name.txt
to see if it is open.
在某些情况下,如果您尝试访问以进行读/写操作的文件被另一个程序打开,则会发生此错误。用lsof | grep the-file-name.txt
,看它是否是开放的。
回答by Athlan
Finally I have solved the problem. One of the directory in path haven't executable permission for other group, so as @JustinKSU suggested, there was no possibility to go throught whole path.
最后我解决了这个问题。路径中的目录之一没有其他组的可执行权限,因此正如@JustinKSU 建议的那样,不可能遍历整个路径。
chmod o+x /home/user
solved the problem.
chmod o+x /home/user
解决了这个问题。
回答by Abdull
I ran into this problem during builds within a Jenkins job. I had added the jenkins
user to the tomcat7
group, yet the Jenkins job failed whenever it was supposed to copy artifacts to the Tomcat instance directory.
我在 Jenkins 工作中的构建过程中遇到了这个问题。我已将jenkins
用户添加到tomcat7
组中,但 Jenkins 作业在应该将工件复制到 Tomcat 实例目录时失败。
It turned out all I needed to fix this problem was to restart the Jenkins service.
原来我需要解决这个问题就是重新启动 Jenkins 服务。
回答by MonoThreaded
Same symptoms when you have selinux
activated
You can check the status with sestatus
and disable it with setenforce 0
It may solve your problem in the short term, just make sure it's reboot proof.
selinux
激活后的相同症状
您可以检查状态sestatus
并禁用setenforce 0
它可能会在短期内解决您的问题,只需确保它是重启证明。