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

Get the Currently Logged in User

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

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

This is also platform-independent.

Get the Hostname of the Machine

1
2
3
4
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
String hostname = localMachine.getHostName();

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