java 8 can not get ubuntu hostname -


i using java version "1.8.0_65",ubuntu server 16.04.

when use following code,the add.gethostname() returns same add.gethostaddress().

please me this.

public class test {     public static void main(string[] args) throws exception{         inetaddress add = findfirstnonloopbackaddress();         system.out.println("=====================");         system.out.println("hostname:" + add.gethostname());         system.out.println("ip:" + add.gethostaddress());         system.out.println("=====================");     }     public static inetaddress findfirstnonloopbackaddress() {         inetaddress result = null;         try {             int lowest = integer.max_value;             (enumeration<networkinterface> nics = networkinterface.getnetworkinterfaces(); nics.hasmoreelements(); ) {                 networkinterface ifc = nics.nextelement();                 if (ifc.isup()) {                     system.out.println("testing interface: " + ifc.getdisplayname());                     if (ifc.getindex() < lowest || result == null) {                         lowest = ifc.getindex();                     } else if (result != null) {                         continue;                     }                     (enumeration<inetaddress> addrs = ifc.getinetaddresses(); addrs.hasmoreelements(); ) {                         inetaddress address = addrs.nextelement();                         if (address instanceof inet4address && !address.isloopbackaddress()) {                             system.out.println("found non-loopback interface: " + ifc.getdisplayname());                             result = address;                         }                     }                 }             }         } catch (ioexception ex) {             system.err.println("cannot first non-loopback address" + ex);         }         return result;     } } 

edit:

root@test# cat /etc/network/interfaces # file describes network interfaces available on system # , how activate them. more information, see interfaces(5).  source /etc/network/interfaces.d/*  # loopback network interface auto lo iface lo inet loopback  # primary network interface auto ens32 # iface ens32 inet dhcp iface ens32 inet static address 192.168.1.37 hostname test-7 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameserver 192.168.1.1 

edit2:

root@test# cat /etc/hosts 127.0.0.1       localhost 127.0.1.1       test-7  # following lines desirable ipv6 capable hosts ::1     localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 


Comments