Java:确定当前 Windows 用户的编程方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31394/
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
Java: Programatic Way to Determine Current Windows User
提问by jjnguy
I see many similar questions, however I want to find the Username of the currently logged in user using Java.
我看到很多类似的问题,但是我想使用 Java 查找当前登录用户的用户名。
Its probably something like:
它可能是这样的:
System.getProperty(current.user);
But, I'm not quite sure.
但是,我不太确定。
回答by Chris Bunch
You're actually really close. This is what you're looking for:
你真的很亲近。这就是你要找的:
System.getProperty("user.name")
回答by John Meagher
The commonly available system properties are documented in the System.getProperties()
method.
System.getProperties()
方法中记录了常用的系统属性。
As Chris said "user.name"
is the property to get the user running your app.
正如克里斯所说"user.name"
,这是让用户运行您的应用程序的属性。
回答by marklark
As mentioned above (and linked for Java 6), to get the current user:
如上所述(并为 Java 6 提供链接),获取当前用户:
System.getProperty("user.name")
For Java 7: System.getProperties()
对于 Java 7: System.getProperties()
For Java 8: System.getProperties()
对于 Java 8: System.getProperties()
For Java 9: System.getProperties()
对于 Java 9: System.getProperties()