6

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?

Rohan
  • 521
  • 4
  • 9
  • 24
  • 1
    Do you mean a user logged into Windows? Or a user logged into your web application (from a browser somewhere out in the world)? – Thilo Aug 21 '13 at 09:37
  • 1
    @Duncan No.its just standalone application.no server – Rohan Aug 21 '13 at 09:37
  • 1
    I am getting my username when i run this as part of a standalone java program – Juned Ahsan Aug 21 '13 at 09:38
  • @Thilo i mean user logged into windows – Rohan Aug 21 '13 at 09:39
  • 1
    Maybe helpful: http://stackoverflow.com/a/16219490/14955 – Thilo Aug 21 '13 at 09:40
  • @Juned i was also getting that before some days back but now its not working.Thats why i want to know how exactly System.getProperty works on windows. On windows server 2008, environment variable USERNAME has value SYSTEM – Rohan Aug 21 '13 at 09:41
  • @EJP earlier i was getting proper user name on windows server 2008 but from last few days its returning HOSTNAME of machine insted of user name – Rohan Aug 21 '13 at 10:00

2 Answers2

6

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

Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14
  • It says USERNAME=admin – Rohan Aug 21 '13 at 09:52
  • Strange... anything special how your application is started? Maybe try Process Explorer from www.systernals.com to check what the actual environment of your running process is. – Gyro Gearless Aug 21 '13 at 10:01
  • Hmm, what do you mean with " i am passing command to that jar from my local machine"? With exactly what command do you invoke it? – Gyro Gearless Aug 21 '13 at 11:46
  • i am invoking JAR file placed on windows server by passing command with JSCH EXEC channel and code given in the question is present in that jar file.Here if i am launching that jar file by directly copy paste this command on the command promt of windows server then its properly giving user.name but when i am executing this by passing command through JSCH EXEC channel its giving me op as INPUSCPC which is nothing but host name of that server – Rohan Aug 21 '13 at 11:52
  • @Rohan: sounds like JSCH EXEC (whatever that is) is running your code in the context of the server's domain account. Or perhaps is just setting up the environment in an odd way. At any rate, that's where you need to look for a resolution. – Harry Johnston Aug 22 '13 at 05:33
0

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);
        }
   }
Sarath
  • 21
  • 4
  • When i tried this one i am getting strage output.as user.name=>>>>Scripts.jar and java.class.path=>>>>INPUSCPC08216$ here Script.jar is the name of the jar file on windows server which i am trying to run from my local machine and when i am using user.name it is giving me output as INPUSCPC08216$ – Rohan Aug 21 '13 at 11:09
  • Can you give more explanation on what you are trying the get from the property name `user.name` or `java.class.path` – Sarath Aug 21 '13 at 11:18
  • i am trying user.name and i am getting INPUSCPC08216$ which is nothing but the values of java.class.path as mentioned above – Rohan Aug 21 '13 at 11:25
  • Now check with this edited code. you should be getting `user.name` correclty – Sarath Aug 21 '13 at 11:42