176

Is it possible to get the name of the currently logged in user (Windows/Unix) and the hostname of the machine?

I assume it's just a property of some static environment class.

I've found this for the user name

com.sun.security.auth.module.NTSystem NTSystem = new
        com.sun.security.auth.module.NTSystem();
System.out.println(NTSystem.getName());

and this for the machine name:

import java.net.InetAddress;
...
String computerName;
...
try {
    computerName = InetAddress.getLocalHost().getHostName();
}

catch(Exception ex) {
    ...
}

Is the first one just for Windows?

And what will the second one do, if you don't have a hostname set?

1
  • The NTSystem class only exists on Windows JDK distributions Mar 31, 2017 at 20:09

4 Answers 4

286

To get the currently logged in user:

System.getProperty("user.name"); //platform independent 

and the hostname of the machine:

java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());
4
  • 4
    Does System.getProperty("user.name"); work on both windows and linux?
    – Catfish
    Apr 2, 2014 at 20:15
  • 8
    However, the user.name value can be spoofed, so it should not be used upon for authentication.
    – Raedwald
    Jan 12, 2015 at 10:26
  • 20
    System.getProperty("user.name") is NOT currently logged in user, it's user under who's security context is JVM running.
    – rkosegi
    Sep 30, 2015 at 11:45
  • 2
    As for what OP refers to as "machine name" or "computer name" this answer is incorrect. Java has no way to obtain the "computer name", i.e. the name assigned to the computer early on in the boot process and unrelated to network. All OS'es have this concept, but unfortunately this value is not exposed in Java. However often - but not always - the above method will indeed return the computer name. See stackoverflow.com/a/40702767/1504556 for explanation.
    – peterh
    Nov 30, 2016 at 17:19
106

To get the currently logged in user:

System.getProperty("user.name");

To get the host name of the machine:

InetAddress.getLocalHost().getHostName();

To answer the last part of your question, the Java API says that getHostName() will return

the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.

2
  • 1
    Upvote for answering all of the OPs original questions bar one clearly and concisely. Surely this should be the accepted answer
    – Andy
    Jan 1, 2013 at 21:53
  • 1
    Agree with @Andy - up-vote for clear and concise answer to all points.
    – Paul Eden
    Jan 30, 2014 at 20:54
11

Using user.name is not secure as environment variables can be faked. Method you were using is good, there are similar methods for unix based OS as well

4
  • So this is just for windows? Is there a platform independent way to do this? Sep 24, 2012 at 13:25
  • 8
    System properties are not environment variables. Use System.getenv("USER") for environment variables. System.properties can still be set by user on java commandline with java -Duser.name=someoneelse so still not secure
    – dan carter
    Aug 23, 2013 at 2:51
  • "There are similar methods for unix based OS as well": Thanks, what i was looklng for. So what is the method for these OS? my research, currently conducted me ... here :) using "whoami" ?
    – pdem
    Aug 26, 2016 at 8:02
  • What is "secure" methods that cannot be faked? Is it exists?
    – wirekang
    Nov 20, 2023 at 1:56
2

To get the currently logged in user path:

System.getProperty("user.home");

Not the answer you're looking for? Browse other questions tagged or ask your own question.