So what I am trying to do is let my Java find the user's name that windows is logged in with, so when I would say such a method, it would return the users name, like I use it in the User called Noah, java would return "Noah" and if I were on the user Amanda, Java would return "Amanda". How would I do this?
Asked
Active
Viewed 8.7k times
39
-
Possible duplicate of [Java current machine name and logged in user?](http://stackoverflow.com/questions/473446/java-current-machine-name-and-logged-in-user) – user3004449 Apr 10 '17 at 07:53
-
Possible duplicate of https://stackoverflow.com/questions/797549/get-login-username-in-java – kevinarpe Jun 22 '21 at 10:08
4 Answers
68
Lookup the system property "user.name".
String username = System.getProperty("user.name");
Demonstration: Main.java
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("user.name"));
}
}
Output:
c:\dev\src\misc>javac Main.java
c:\dev\src\misc>java Main
rgettman
c:\dev\src\misc>

rgettman
- 176,041
- 30
- 275
- 357
-
9This is how answers should be. This fixed my current problem, _and_ taught me extras as a bonus. – jumps4fun Apr 28 '14 at 11:50
20
Two ways
System.getProperty("user.name");
System.getenv("USERNAME");
Both are good for any OS

wilx
- 17,697
- 6
- 59
- 114

Evgeniy Dorofeev
- 133,369
- 30
- 199
- 275
-
The environment variable `USERNAME` might be available on different versions of Windows, but it is NOT available by default on macOS (checked Catalina) or Linux/GNU (checked Linux Mint). You'd need to check `USER` instead on those systems. – neuralmer Apr 01 '20 at 20:52
18
Try:
String userName = System.getProperty("user.name");
or
String userName = new com.sun.security.auth.module.NTSystem().getName()

user987339
- 10,519
- 8
- 40
- 45
-
6I like this response because when some java apps run as a Windows service, System.getProperty("user.name") returns "SYSTEM" if the service started before the user logged in and not the currently logged user at the time the call is made. NTSystem.getName() returns the currently logged username at the time of the call. The native method is useful in implementing logic that is Windows specific and where people run into user "SYSTEM" returned by System.getProperty("user.name") when running as a windows service. – Sanjiv Jivan Jun 24 '14 at 14:34
-
@SanjivJivan For me `new NTSystem().getName()` returns `SYSTEM` running as a windows service with jre1.8.0_201 before any and also after log on with a windows user. – irieill Mar 29 '19 at 12:00
-
Be aware that using System.getProperty("user.name"); is not safe, it is very easy to spoof, like using -Duser.name=Admin or changing it in the command line. – Adrião Neves Sep 15 '20 at 17:20
1
NTSystem.getName() also returns SYSTEM when the app runs on a windows service. There is no means to get the username with NTSystem when the app is running on a windows service

aaa
- 19
- 1
-
as @Sanjiv Jivan said above: System.getProperty("user.name") returns "SYSTEM" if the service started before the user logged in and not the currently logged user at the time the call is made. NTSystem.getName() returns the currently logged username at the time of the call. The native method is useful in implementing logic that is Windows specific and where people run into user "SYSTEM" returned by System.getProperty("user.name") when running as a windows service. – GMLewisII Apr 19 '17 at 20:54