Here you can find the source of getHostnameFromNetworkInterface()
public static synchronized String getHostnameFromNetworkInterface()
//package com.java2s; /*/*from w w w . j a v a 2s. c o m*/ * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.net.InetAddress; import java.net.NetworkInterface; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class Main { /** host name (from network intf) for faster access. */ protected static String m_HostnameNetworkInterface; /** * Returns the host name determined from the network interfaces. * * @return the host name, null if none found */ public static synchronized String getHostnameFromNetworkInterface() { String result; List<String> list; Enumeration<NetworkInterface> enmI; NetworkInterface intf; Enumeration<InetAddress> enmA; InetAddress addr; boolean found; result = null; if (m_HostnameNetworkInterface == null) { list = new ArrayList<>(); found = false; try { enmI = NetworkInterface.getNetworkInterfaces(); while (enmI.hasMoreElements()) { intf = enmI.nextElement(); // skip non-active ones if (!intf.isUp()) continue; enmA = intf.getInetAddresses(); while (enmA.hasMoreElements()) { addr = enmA.nextElement(); list.add(addr.getHostName()); if (addr.getHostName().indexOf(':') == -1) { result = addr.getHostName(); found = true; break; } } if (found) break; } } catch (Exception e) { // ignored } if (result == null) { if (list.size() > 0) result = list.get(0); else result = "<unknown>"; } m_HostnameNetworkInterface = result; } else { result = m_HostnameNetworkInterface; } return result; } }