Categories: Network

Java get hostname and IP address

Dec 19, 2017 1 min.

Use java.net.InetAddress to get hostname or IP Address To get local hostname or IP import java.net.InetAddress; public class LocalIPGetter { public static void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.getLocalHost(); // Get Hostname System.out.println(inetAddress.getHostName()); // Get IP Address System.out.println(inetAddress.getHostAddress()); } } $javac LocalIPGetter.java $java LocalIPGetter mytestpc 172.17.0.2 To get remote hostname or IP import java.net.InetAddress; public class RemoteIPGetter { public static void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.