Search code examples
javalinuxpermissionslinux-mint

/usr/local and subfolders are not recognized by my Java program


I have got a 64-bit Linux Mint 20.1 OS and I realized that my arbitrary Java program does not recognize "/usr/local" or anything in it as an existing entity. To clarify what I mean, I have stated a couple of examples below:

System.out.println(new File("/usr").exists()); //prints true

System.out.println(new File("/usr/local").exists()); //prints false

System.out.println(new File("/usr/local/bin").exists()); //prints false

System.out.println(new File("/usr/local/random-file-1").exists()); 
//prints false, regardless of whether that file exists

I have checked the permissions of /usr/local, supposing that something might be amiss there, but oddly, I haven't found anything wrong:

Could you give your two cents on what could go pear-shaped?


Solution

  • Has nothing to do with java and everything to do with your linux installation:

    Option 1: No Unix UGO x rights on this dir.

    The process is not capable of seeing into the /usr directory. Most likely, the process runs as user X, which is in groups [Y] (can be in multiple!).

    The /usr directory is surely not owned by X (on all relevant unix distros, it is owned by root). It's also likely that /usr's group owner is anything in the [Y] set either; on most linuxen it is group root, and no users are part of this group except root itself.

    Thus, the last 3 letters of the access rights apply (it's user/group/others). So, run ls -lah / and check the access rights of /usr. I expect:

    drwxr-xr--  10 root root 4.0K Jan 1   2021 usr
    

    Note that r-- at the end. no x means no rights to even look in it. That would explain what you're witnessing.

    Note that this has nothing to do with the access rights of /usr/local! To read dirs you need access to the entire 'stack', and in general the dir that blocks you is still, itself, visible to you - it's just that you can't delete or move it, can't run or open anything in it, and can't even ls it. Thus, the fact that you can't even 'see' /usr/local suggests that, if it's UGO access at all, it's not /usr/local, but /usr or /.

    Option 2: ACL overlay rights

    Some distros (notably, mac OS X) add a layer of access rights on top of the standard unixy user/group + UGO rights system. For example, in mac OS, you need to go to the system preferences pane and go to 'privacy', and add some disk acces rights here. I'm not sure if mint has a similar system. Probably not, especially for /usr which is a rather important one.

    Option 3: Weird stuff

    You're using outdated java APIs that you shouldn't be using anymore. One of the downsides of this API is that it cannot give you information about stuff, it can only return booleans. So, try with the new API and if you get an error, because if you do, it'll help A LOT more: It'll have a lot of text to explain it to you:

    (First check that /usr/bin/man exists; it should. If not, pick any other existing thing you find there).

    import java.nio.file.*;
    
    class Test {
        public static void main(String[] args) throws Exception {
            Path p = Paths.get("/usr/bin/man"); // I assume this exists.
            byte[] b = Files.readAllBytes(p);
            System.out.println("size of man exe: " + b.length);
        }
    }
    

    This will throw something (or if it does not, hey, something else is going wrong because this test app is having no problem with it!) - the text of that exception will tell you something useful.

    Option 4: Uhoh

    Your disk is corrupt. Let's just hope this isn't it :P