Rohan
Rohan

Reputation: 561

System.getProperty("user.name") returns HOSTNAME instead of currently logged username

Here

System.getProperty("user.name"); 

returns host-name of windows server 2008 machine instead of currently logged in user name.

Below is my code

final String user = System.getProperty("user.name");
logger.info("User Name : " + user);

I want to know how System.getProperty works in java and on windows server 2008? and why is it returning wrong value in this case?

Upvotes: 7

Views: 29862

Answers (2)

Sarath
Sarath

Reputation: 21

to display list of all properties that are set in java, try the below code

   public static void main(String[] args)
   {
        Properties prop = System.getProperties();
        Set<String> a = prop.stringPropertyNames();
        Iterator<String> keys = a.iterator();
        while (keys.hasNext())
        {
            String key = keys.next();
            String value = System.getProperty(key);
            System.out.println(key + "=" + value);
        }
   }

Upvotes: 0

Gyro Gearless
Gyro Gearless

Reputation: 5279

Just checked this: System.getProperty("user.name"); returns the value from environment variable USERNAME, so check what set USERNAME says in CMD window

Upvotes: 7

Related Questions