private static String getPreferredHostName() { List<InetAddress> addresses = Ordering.natural().onResultOf(InetAddressScope::of).reverse().sortedCopy(getLocalAddresses()); if (addresses.isEmpty()) { return "localhost"; } /* For legibility, we prefer to see a traditional * DNS host name; but if there is no mapping, * use the first address. */ for (InetAddress a: addresses) { String hostName = stripScope(a.getCanonicalHostName()); if (!InetAddresses.isInetAddress(hostName)) { return hostName; } } return addresses.get(0).getCanonicalHostName(); }