Jump to content

Java Hostname Lookup

From EggeWiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

The following snippet shows how to get the hostname and IP address.

	public static void main(String[] args) throws UnknownHostException {
		java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
		System.out.println("Hostname of local machine: " + localMachine.getCanonicalHostName());
		System.out.println("IP of local machine: " + localMachine.getHostAddress());
		java.net.InetAddress localhost = java.net.InetAddress.getByName("localhost");
		System.out.println("Hostname of localhost: " + localhost.getCanonicalHostName());
		System.out.println("IP of localhost: " + localhost.getHostAddress());
	}

Hostname of local machine: mail.theeggeadventure.com
IP of local machine: 67.207.137.114
Hostname of localhost: localhost
IP of localhost: 127.0.0.1