1

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?

  • Java invokes the host operating system to resolve dns; why would there be a specific Java DNS resolver by default? The only option I know of would be running your own Java DNS. Which is probably not what you want. Why do you want to do this? – Elliott Frisch Oct 29 '22 at 08:16
  • I was interested in it for educational purposes. How can I run my Java DNS which works for java version 8 and later? Could you provide a sample code? – Edgar Becker Oct 29 '22 at 08:22
  • No one is going to provide "sample code", because no one is doing this. [dnsjava](https://github.com/dnsjava/dnsjava) is a dns server written in Java. You can run any dns server you like, [Unbound](https://www.nlnetlabs.nl/projects/unbound/about/) for example, then you must configure your operating system to use it. Note: This is still not something Java (as a platform) is designed for. Why would there be a "resolve dns names" in an atypical manner? – Elliott Frisch Oct 29 '22 at 08:31
  • Rather than starting a server I was wondering if there was a solution similar to [this](https://stackoverflow.com/questions/11647629/how-to-configure-hostname-resolution-to-use-a-custom-dns-server-in-java#answer-67683205) but also working for Java 8. If we could do the same programmatically it would be even better instead of passing a parameter to the JDK. – Edgar Becker Oct 29 '22 at 08:42
  • Not that I'm aware of. Good luck! – Elliott Frisch Oct 29 '22 at 09:02

1 Answers1

1

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