How to work around the hosts file during an nslookup in Java using InetAddress

advertisements

Is it possible to force Java to ignore the hosts file when doing a DNS lookup (or reverse DNS lookup)?

I have been looking into how to perform an nslookupin Java for quite a while, and all examples I can find end up using some variation of this command:

InetAddress.getByName("123.12.12.12").getCanonicalHostName();

The problem I'm having is that the response being returned is taken from the Windows hosts file, not a true nslookup. If I remove the entry from my hosts file, then I get the correct result returned, but as soon as I put the entry back in the hosts file, the program returns the value from the hosts file.

If you're wondering why I'm trying to do this it is because I need to run this program on a server where I don't have the ability to modify the hosts file - it is a production server at a large company, and modifications to the hosts file will cause many applications to break. The problem is that I need to get the actual host name which would be returned from nslookup, not the value from the hosts file.


I don't know how to change this behaviour through the InetAddress class.

Java Doc InetAddress

Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS)

It looks like Java is relying on the resolver library. Understanding host name resolution and DNS behavior in Java

Maybe could you try to configure the Name Service Switch configuration file nsswitch.conf in order to reorder the resolver strategy ?