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
It's not ok to just return the first non-loopback interface as it might have been created by some software like Parallels. It's a better bet to try fishing for the eth0.
static private InetAddress getIPv4InetAddress() throws SocketException, UnknownHostException {
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) {
NetworkInterface ni = NetworkInterface.getByName("eth0");
Enumeration ias = ni.getInetAddresses();
InetAddress iaddress;
do {
iaddress = ias.nextElement();
} while(!(iaddress instanceof Inet4Address));
return iaddress;
}
return InetAddress.getLocalHost(); // for Windows and OS X it should work well
}