Java current machine name and logged in user


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 NTSystem = new;

and this for the machine name:

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?

Best Solution

To get the currently logged in user:

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

and the hostname of the machine: localMachine =;
System.out.println("Hostname of local machine: " + localMachine.getHostName());