11

For testing we have a DNS server that will respond with dummy records. Previously we could make java use our DNS server (here it is just using local host) by using:

"-Dsun.net.spi.nameservice.nameservers=127.0.0.1",
"-Dsun.net.spi.nameservice.provider.1=dns,sun",

This no longer works under jdk11. Is it possible to specify the DNS server to use under jdk11? If so how?

Edit: I also attempted:

-Djava.naming.provider.url=dns://127.0.0.1

from https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/jdk.naming.dns/share/classes/com/sun/jndi/dns/DnsContextFactory.java but that did not work either.

Luke
  • 884
  • 8
  • 21
  • 1
    That "feature" was removed in JDK 9, here's the release note on the issue: https://www.oracle.com/technetwork/java/javase/9-removed-features-3745614.html#JDK-8134577 – Alan Bateman Apr 01 '19 at 07:07
  • 1
    To handle it, I would rather encourage you to setup the DNS server at the machine level instead of updating it in java. – mdeora Apr 01 '19 at 11:37
  • @AlanBateman so it is impossible, would you please put that in as the answer to this question. – Luke Apr 01 '19 at 22:29

1 Answers1

5

Defining an alternative DNS server is already not supported: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8134577

For testing you should use an alternative hosts file that could be defined by the -Djdk.net.hosts.file=path_to_alternative_hosts_file JVM option.

For example if you have a file with following content at D:\test\hosts

10.20.30.40        google.com www.google.com

Running this code with -Djdk.net.hosts.file=D://test/hosts

    public static void main(String[] args) throws UnknownHostException {
        InetAddress address = InetAddress.getByName("google.com");
        System.out.println(address);
    }

will print:

google.com/10.20.30.40
Rostislav Krasny
  • 577
  • 3
  • 14