Take the 2-minute tour ×

Is there a way to view/dump DNS cached used by java.net api?

share|improve this question
My understanding is you have to have a DNS server to perform caching - a host itself does not cache DNS requests. –  OMG Ponies Dec 2 '09 at 20:05
No the host caches. Indeed the JRE (running with a security manager) and browsers "pin" DNS lookups. –  Tom Hawtin - tackline Dec 2 '09 at 20:23
dns resolver libraries typically caches dns results so they don't have to bother the server so much, and provide quicker response to calls –  nos Dec 2 '09 at 20:42
I don't think java.net does any DNS caching -- this is probably handled by the OS and may not be directly accessible. –  Jim Garrison Dec 2 '09 at 20:43
Hmmm.... A little google-fu proves me wrong. See for instance verisign.com/stellent/groups/www_ndscs/documents/… –  Jim Garrison Dec 2 '09 at 20:45
add comment

2 Answers

The java.net.InetAddress uses caching of successful and unsuccessful host name resolutions.

From its javadoc:





  • networkaddress.cache.ttl

    A value of -1 indicates "cache forever".

  • networkaddress.cache.negative.ttl (default: 10)


If what you have in mind is dumping the caches (of type java.net.InetAddress$Cache) used by java.net.InetAddress , they are internal implementation details and thus private:

 * Cached addresses - our own litle nis, not!
private static Cache addressCache = new Cache(Cache.Type.Positive);

private static Cache negativeCache = new Cache(Cache.Type.Negative);

So I doubt you'll find anything doing this out of the box and guess that you'll have to play with reflection to achieve your goal.

share|improve this answer
add comment
up vote 15 down vote accepted

Here is a script to print the positive and negative DNS address cache.

import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class DNSCache {
    try {
    } catch (UnknownHostException e) {


    String addressCache = "addressCache";
    String negativeCache = "negativeCache";
  private static void printDNSCache(String cacheName) throws Exception {
    Class<InetAddress> klass = InetAddress.class;
    Field acf = klass.getDeclaredField(cacheName);
    Object addressCache = acf.get(null);
    Class cacheKlass = addressCache.getClass();
    Field cf = cacheKlass.getDeclaredField("cache");
    Map<String, Object> cache = (Map<String, Object>) cf.get(addressCache);
    for (Map.Entry<String, Object> hi : cache.entrySet()) {
        Object cacheEntry = hi.getValue();
        Class cacheEntryKlass = cacheEntry.getClass();
        Field expf = cacheEntryKlass.getDeclaredField("expiration");
        long expires = (Long) expf.get(cacheEntry);

        Field af = cacheEntryKlass.getDeclaredField("address");
        InetAddress[] addresses = (InetAddress[]) af.get(cacheEntry);
        List<String> ads = new ArrayList<String>(addresses.length);
        for (InetAddress address : addresses) {

        System.out.println(hi.getKey() + " "+new Date(expires) +" " +ads);
share|improve this answer
Reflection?​​​​​ –  Pacerier Jul 25 '12 at 8:50
Nice sample :-) –  Johnny Lim Jul 30 '12 at 8:17
In case someone want to run the above code using a security manager. (or converting it to run in a servlet engine which is running using a security manager), following entries in policy file will help: grant { permission java.lang.RuntimePermission "accessClassInPackage.sun.net"; permission java.lang.RuntimePermission "accessDeclaredMembers"; permission java.lang.reflect.ReflectPermission "suppressAccessChecks"; permission java.net.SocketPermission "*","accept,connect,resolve"; –  Usman Saleem Dec 19 '12 at 3:41
add comment

Your Answer


