Obtaining the ip and hostnameFrom WikiJava
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
the exampleimport 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()); } } |
