The java.net.InetAddress class represents an IP address. It provides the methods to work with IP and host name.
Get ip address in java
package com.w3spoint;
import java.net.InetAddress;
public class NetworkTest {
public static void main(String args[]){
try {
InetAddress inetAddress = InetAddress.getLocalHost();
System.out.println(inetAddress.getHostAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
} |
package com.w3spoint; import java.net.InetAddress; public class NetworkTest {
public static void main(String args[]){
try {
InetAddress inetAddress = InetAddress.getLocalHost();
System.out.println(inetAddress.getHostAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output