How to Get the Current Machine Name and Logged in User in Java

  • Home /
  • Blog Posts /
  • How to get the Current Machine Name and Logged In User in Java

It’s very easy to get the current machine name and logged in user using Java.

Get the Currently Logged in User

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

System.out.println("Username logged in: " + username);

This is also platform-independent.

Get the Hostname of the Machine

java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
String hostname = localMachine.getHostName();

System.out.println("Hostname of local machine: " + hostname);