Java system.getProperty issue

1.7k Views Asked by At

I found that on my OSX system I have "weird" setup. Java reads my user name as "root" instead of my actual user name. Turns out the code I used uses System.getPropery("user.name") call. So, I made up stand-alone snipped (I save it under test.java) which prints java system user.name property and my UNIX user name environment:

class UserName {
public static void main(String[] args) {
   String name;
   name = System.getProperty("user.name");
   System.out.println("user.name="+name);
   name = System.getenv("USER");
   System.out.println("USER="+name);
}
}

if I compile it as javac test.java and then run it (under my account, not root one) as java UserName it prints the following:

user.name=root
USER=vk

My question is where/how java sets user.name and why it is set to root instead of my actual UNIX name. I'm not java expert and don't really know internals of System.getProperty API.

The second question is it possible to change this settings somehow in UNIX environment that it will read properly my user name.

Finally may be my environment or java configuration is broken and there is some hidden file (properties) which should be fixed.

3

There are 3 best solutions below

0
On

The problem was resolved by turning off setuid bit in java executable. Thanks goes to David Conrad for correct tip. For the reference:

ls -l /usr/bin/java
-rwsr-xr-x ... /usr/bin/java
sudo chmod -s /usr/bin/java
lrwxr-xr-x ... /usr/bin/java

and after that everything was reported properly.

0
On

Java will get the user.name property from the operating system, so if you are not running as root or using sudo to start java, then the java executable must be setuid root.

You can check for this by doing:

$ ls -l /path/to/java

If java is setuid root, you will see something like:

-rwsr-xr-x 1 root root 123456 Apr 1 16:59 /path/to/java

The 's' in the user execute bit indicates that it is a setuid program. You can remove the setuid bit with:

$ sudo chmod -s /path/to/java

5
On

You are running java with sudo or after you run su command and become root. (Maybe MacOS by default run your commands as root since your user in an administrators group).

Same output is also obtained on Linux (CentOS) when run after su command with root privileges.

user.name=root
USER=jdiver