Search code examples
javanetworkingdnsinetaddress

How to configure host name resolution to use a custom host resolver that works in Java 8 and later?


According to this post there are several ways to configure a custom host resolver in Java, but each of these ways differs from the other and does not work for all java versions between 8 and the most recent.

The post indicated above is 10 years old: does anyone know if in this period a solution has been found that works for all the java versions mentioned above? If so, does anyone have an example code?


Solution

  • After a deep search I found this example that uses the burningwave tools library:

    Map<String, String> hostAliases = new LinkedHashMap<>();
    hostAliases.put("my.hostname.one", "123.123.123.123");
    
    HostResolutionRequestInterceptor.INSTANCE.install(
        new MappedHostResolver(hostAliases),
        DefaultHostResolver.INSTANCE
    );
    
    InetAddress inetAddress = InetAddress.getByName("my.hostname.one");
    

    And here the code to add a DNS server connection based host resolver