java.io.FileNotFoundException (permission denied)

2019-02-08 07:09发布

问题:

I have faced strange poblem while writing Grails application deployed on Tomcat.

After creating simple test controller I want to write test contents in package 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)

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:

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?

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.

I am very investigative and cannot understand the behaviour.

回答1:

Ensure you that you have read and execute access to all parent directories as well.

Example: chmod o+x /home/user



回答2:

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.

chmod o+x /home/user solved the problem.



回答3:

FileNotFoundException occurs:

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.



回答4:

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.

It turned out all I needed to fix this problem was to restart the Jenkins service.



回答5:

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.