How do I get a localhost hostname?
Date: 2010-09-16. Category: Java Networking examples. Hits: 10K time(s).
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostnameExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("Hostname: " + address.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}