Wednesday, 25th April 2012
Follow WikiJava on twitter now. @Wikijava

Obtaining the ip and hostname

From WikiJava

Jump to: navigation, search


This example shows how it's possible in java to obtain the hostname for the host where the JVM is running. it's very simple, and it uses the InetAddress Class

Contents

the example

import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class Myhostname {
 
	public static void main(String[] args) {
		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return;
		}
 
		StringBuilder sb = new StringBuilder();
		sb.append("ip: ").append(addr.getHostAddress());
		sb.append("\nhostname: ").append(addr.getCanonicalHostName());
		System.out.println(sb.toString());
	}
 
}

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.