java.io.FileNotFoundException (permission denied) despite chmod 777
Asked Answered
R

6

25

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.

Roehm answered 8/1, 2014 at 23:44 Comment(6)
Have you checked the permissions of all the subdirectories to make sure that the executor of the Java process can access all of them?Caldeira
@JustinKSU, after adding +x to ALL directories before, all works perfectly. How can I get you correct answer?Roehm
Please tell us what 'Brak dostępu' means. We don't all speak all languages. This site is conducted in English. This message basically contains the answer.Antarctica
Do you have selinux enabled? Can you check the selinux audit log?Cluj
@Roehm Glad you figured it out.Caldeira
@Roehm I added an answer if you want to give me "credit" for it.Caldeira
C
32

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

Example: chmod o+x /home/user

Caldeira answered 9/1, 2014 at 15:38 Comment(3)
It works, but I don't see why read permission on a file depends on the permissions of its parent dir.Nicknack
Java has to traverse the directories to get to the file. If it can't get to the directory, then it will fail to see the file.Caldeira
Use -R to give permissions to subfolders: sudo chmod -R 777 /home/userVolution
R
6

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.

Roehm answered 9/1, 2014 at 12:37 Comment(0)
F
1

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.

Firkin answered 8/1, 2014 at 23:59 Comment(2)
I am sure that file is not used by another process. I have checked it by lsof, but thanks for the solution, hope that it will be usefull for anothers! What is more (and expected), when I run the application and exception occurs, the file is not touched by application (there is no entry in lsof).Roehm
I am very curious what it could be. When you solve this problem post the solution.Firkin
B
1

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.

Bounds answered 30/11, 2015 at 17:38 Comment(0)
B
1

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.

Brio answered 16/11, 2018 at 17:31 Comment(0)
P
1

I'm use virtual box 6.1 and macOS Catalina 10.15.5.

In my case I need to add -R flag. chmod -R o+x /home/user

Phlebitis answered 24/7, 2020 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.