Reputation: 459
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?
Upvotes: 40
Views: 92291
Reputation: 136012
Two ways
System.getProperty("user.name");
System.getenv("USERNAME");
Both are good for any OS
Upvotes: 21
Reputation: 39
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
Upvotes: 3
Reputation: 10707
Try:
String userName = System.getProperty("user.name");
or
String userName = new com.sun.security.auth.module.NTSystem().getName()
Upvotes: 19
Reputation: 178263
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>
Upvotes: 70