40

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?

2

4 Answers 4

70

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

This is how answers should be. This fixed my current problem, and taught me extras as a bonus.
21

Two ways

  1. System.getProperty("user.name");

  2. System.getenv("USERNAME");

Both are good for any OS

1 Comment

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.
19

Try:

String userName = System.getProperty("user.name"); 

or

String userName = new com.sun.security.auth.module.NTSystem().getName() 

3 Comments

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.
@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.
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.
3

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

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.