Search code examples
javalinuxfilepermissionspermission-denied

Getting permission denied in Java when reading a file (Linux)


Hello Everyone i recently moved to Ubuntu to work on a project. I have to simply read from a file, using:

File root = new File(fileroot); File sourceFile = new File(root,filename);

But when i run the application, i get: error: error reading FilePath; FilePath (Permession denied).

(FilePath is the path where the file is located, i've hidden it for privacy purposes)

I guess is something due to the Operative System, because running the same code on Windows doesn't give me any error. Is there a way to solve this? I mean a way to get those permissions directly from Java or maybe changing them from terminal using commands?

Thanks in advance for your answer guys!


Solution

  • Like the message says, you probably don't have permissions to read that file. To troubleshoot, you can add:

    System.out.println(sourceFile.getAbsolutePath());
    

    to find the exact location of the file that you're trying to read and then checking the file system permissions for it:

    ls -la <path to your file>
    

    Verify that you have the r permission, otherwise this is the error that you get.