Empty - By default, if the jdk.net.hosts.file
system property is not defined Java will use the default address resolution.
The jdk.net.hosts.file
system property specifies a file system path to a file that provides DNS name resolution to the InetAddress
methods getByAddress
or getByName
.
The format of this file is the same as a /etc/hosts
file, here's an example:
# comment 127.0.0.1 localhost 10.11.12.13 api.example.com
If the jdk.net.hosts.file
system property points to a file path that does not exist, all DNS resolutions will fail with an UnknownHostException
.
Here are some other networking Java system properties:
http.agent
http.keepAlive
http.maxConnections
https.protocols
java.net.preferIPv4Stack
java.net.preferIPv6Addresses
java.net.useSystemProxies
javax.net.debug
javax.net.ssl.trustStore
jdk.tls.client.protocols
jdk.tls.disabledAlgorithms
networkaddress.cache.negative.ttl
networkaddress.cache.ttl
sun.net.client.defaultConnectTimeout
sun.net.client.defaultReadTimeout
sun.net.inetaddr.ttl
Java has supported the jdk.net.hosts.file
system property since Java 9.
jdk.net.hosts.file
on StartupYou can set the jdk.net.hosts.file
java system property during startup of the java runtime using the -D
command line argument:
java -Djdk.net.hosts.file=/path/to/hosts MyAppMain
You may also be able to specify jdk.net.hosts.file
via the JAVA_TOOL_OPTIONS
environment variable:
JAVA_TOOL_OPTIONS=-Djdk.net.hosts.file=/path/to/hosts
jdk.net.hosts.file
at RuntimeYou can set jdk.net.hosts.file at runtime with the following Java code:
System.setProperty("jdk.net.hosts.file", "/path/to/hosts");
WARNING: Depending on the property and JVM version using
setProperty
may or may not work if the JDK Java class that uses this variable has already been loaded. The value of the jdk.net.hosts.file system property may be cached within an internal private static variable of the implementing class.
To read the value of jdk.net.hosts.file at runtime, you can use this Java code:
String propertyValue = System.getProperty("jdk.net.hosts.file"); if (propertyValue != null) { System.out.println("jdk.net.hosts.file = " + propertyValue); } else { System.out.println("jdk.net.hosts.file was null"); }