How to get the ip of the computer on linux through Java ?
I searched the net for examples, I found something regarding NetworkInterface class, but I can\'t wrap my
This code worked 4me:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class ShowIp {
public static void main(String[] args) throws SocketException {
NetworkInterface ni = NetworkInterface.getByName("eth0");
Enumeration inetAddresses = ni.getInetAddresses();
while(inetAddresses.hasMoreElements()) {
InetAddress ia = inetAddresses.nextElement();
if(!ia.isLinkLocalAddress()) {
System.out.println("IP: " + ia.getHostAddress());
}
}
}
}