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());
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.
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
To get the currently logged in user path:
System.getProperty("user.home");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With