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?
- Possible duplicate of Java current machine name and logged in user?user3004449– user30044492017-04-10 07:53:40 +00:00Commented Apr 10, 2017 at 7:53
- Possible duplicate of stackoverflow.com/questions/797549/get-login-username-in-javakevinarpe– kevinarpe2021-06-22 10:08:13 +00:00Commented Jun 22, 2021 at 10:08
Add a comment |
4 Answers
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> 1 Comment
jumps4fun
This is how answers should be. This fixed my current problem, and taught me extras as a bonus.
Two ways
System.getProperty("user.name");System.getenv("USERNAME");
Both are good for any OS
1 Comment
neuralmer
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.Try:
String userName = System.getProperty("user.name"); or
String userName = new com.sun.security.auth.module.NTSystem().getName() 3 Comments
Sanjiv Jivan
I 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.
irieill
@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.Adrião Neves
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.
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
1 Comment
GMLewisII
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.