You can use the following code:
Inetaddress inet = inetaddress.getlocalhost (); system.out.println ("IP =" INET.GETHOSTADDRESS ());
Work below Window. Returns 127.0.0.1 under Linux. Mainly returned under Linux, the IP address of the LocalHost configured in / etc / hosts, not the network card binding address. Later, you can use the network card's bound address, you can get the IP address of this unit :)
code show as below:
// The card take the machine configuration of IP Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces (); InetAddress ip = null; while (netInterfaces.hasMoreElements ()) {NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement (); System.out.println (ni.getname ()); IP = (inetaddress) ni.getinetaddresses (). nextElement (); if (! ip.issitelocaladdress () &&! ip.isloopBackAddress () && ip.gethostaddress () && ip. ) == - 1) {system.out.println ("IP =" IP.GethostAddress ()); Break;} else {ip = NULL;}}