Java System Properties
Quick Reference Guide


java.net.preferIPv4Stack Java System Property

The java.net.preferIPv4Stack Java System Property accepts a boolean value (true or false)

Default Value

false

java.net.preferIPv4Stack Explained

If the java application prefers to only use IP version 4 (IPv4), then you can set the system property: java.net.preferIPv4Stack=true

When the java.net.preferIPv4Stack property is true, it is impossible for the application to connect to a host that only supports IPv6.

According to the Java Networking Documentation:

If IPv6 is available on the operating system the underlying native socket will be, by default, an IPv6 socket which lets applications connect to, and accept connections from, both IPv4 and IPv6 hosts.

Related System Properties

The java.net.preferIPv6Addresses system property is related to the java.net.preferIPv4Stack property, however it would be used to prefer IPv6 over IPv4 network connections.

Here are some other networking Java system properties:

Supported Since

Java has supported the java.net.preferIPv4Stack system property since at least version 8, support may go back to even older versions of java.

Setting java.net.preferIPv4Stack on Startup

You can set the java.net.preferIPv4Stack java system property during startup of the java runtime using the -D command line argument:

java -Djava.net.preferIPv4Stack=true MyAppMain

You may also be able to specify java.net.preferIPv4Stack via the JAVA_TOOL_OPTIONS environment variable:

JAVA_TOOL_OPTIONS=-Djava.net.preferIPv4Stack=true

Setting / Reading java.net.preferIPv4Stack at Runtime

You can set java.net.preferIPv4Stack at runtime with the following Java code:

System.setProperty("java.net.preferIPv4Stack", "true");

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 java.net.preferIPv4Stack system property may be cached within an internal private static variable of the implementing class.

To read the value of java.net.preferIPv4Stack at runtime, you can use this Java code:

String propertyValue = System.getProperty("java.net.preferIPv4Stack");
if (propertyValue != null) {
    System.out.println("java.net.preferIPv4Stack = " + propertyValue);
} else {
    System.out.println("java.net.preferIPv4Stack was null");
}