Search code examples
javafilejavac

What will be the user name seen by unix for JAVA program which tries to access a non readable file


If I try to create a FileInputStream on a file which is read protected

new FileInputStream(new File("temp.txt"))

then I get a FileNotFoundException with the message Access is Denied.

What would be the user name as seen by the operating system for that java program?


Solution

  • The OS will never actually see a user name, but instead in the case of Unix like systems it will see a UID (user ID). This will be the UID of whatever user is the owner of the JVM process that's running the application.

    This may be the user that's currently logged in, or if a setuid bit is configured in the ACLfor the JVM's executable (or within the security policy for the system elsewhere), then this may run with privileges of another user per the policy (essentially the JVM will appear to be running as a different user).

    In short, it depends on the configuration of your system.


    In Java to get a string that represents the current user that you're applicaton is running under you can use the following command:

    System.getProperty("user.name");
    

    This will work regardless of the OS you're running. You can then print this to the console or somewhere else you have access. If this question is just based on troubleshooting the access denied issue, then I'd recommend printing this to the console, and then making sure that this user has read/write permissions to whatever directory that you're attempting to read/write from/to.