Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure the DNS server java should use in jdk 9, 10 and 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.

like image 592
Luke Avatar asked Mar 31 '19 23:03

Luke


1 Answers

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
like image 186
Rostislav Krasny Avatar answered Oct 22 '22 14:10

Rostislav Krasny