Java System.getProperty("user.name") 返回 HOSTNAME 而不是当前登录的用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18353856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:49:01  来源:igfitidea点击:

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

javawindows

提问by Rohan

Here

这里

System.getProperty("user.name"); 

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

返回 Windows Server 2008 机器的主机名,而不是当前登录的用户名。

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?

我想知道 System.getProperty 如何在 java 和 windows server 2008 上工作?为什么在这种情况下它返回错误的值?

回答by Gyro Gearless

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

刚刚检查了这个:System.getProperty("user.name");从环境变量返回值USERNAME,所以检查set USERNAMECMD 窗口中的内容

回答by Sarath

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

要显示在 java 中设置的所有属性的列表,请尝试以下代码

   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);
        }
   }