Magno C
Magno C

Reputation: 2196

Why is Java getting the cached version of the host name?

Please refer to this question:

https://askubuntu.com/questions/758432/is-there-another-place-to-host-machine-name?noredirect=1#comment1132454_758432

As you can see, I'm using this method to get the machine name:

private InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = en.nextElement();
        for (Enumeration<InetAddress> en2 = i.getInetAddresses(); en2.hasMoreElements();) {
            InetAddress addr = en2.nextElement();
            if (!addr.isLoopbackAddress()) {
                if (addr instanceof Inet4Address) {
                    if (preferIPv6) {
                        continue;
                    }
                    return addr;
                }
                if (addr instanceof Inet6Address) {
                    if (preferIpv4) {
                        continue;
                    }
                    return addr;
                }
            }
        }
    }
    return null;
} 

The caller:

InetAddress ip = getFirstNonLoopbackAddress(true, false);
this.machineName = ip.getCanonicalHostName();

But the result is I'm getting the old machine name, before I change it in Ubuntu.

How can I get the real machine/host name as in /etc/hostname file?

Upvotes: 0

Views: 633

Answers (1)

victor sosa
victor sosa

Reputation: 899

The dns always do cache of names. You should do a flush dns in the OS that you are using; On Ubuntu I found this about how to flush the dns:

https://askubuntu.com/questions/414826/how-to-flush-dns-in-ubuntu-12-04

Or try this approach: Flush DNS using Java

Upvotes: 2

Related Questions