How to set java.net.preferIPv4Stack=true at runtime?
Solution 1
You can use System.setProperty("java.net.preferIPv4Stack" , "true");
This is equivalent to passing it in the command line via -Djava.net.preferIPv4Stack=true
Solution 2
Another approach, if you're desperate and don't have access to (a) the code or (b) the command line, then you can use environment variables:
http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/plugin.html.
Specifically for java web start set the environment variable:
JAVAWS_VM_ARGS
and for applets:
_JPI_VM_OPTIONS
e.g.
_JPI_VM_OPTIONS=-Djava.net.preferIPv4Stack=true
Additionally, under Windows global options (for general Java applications) can be set in the Java control plan page under the "Java" tab.
Solution 3
I ran into this very problem trying to send mail with javax.mail from a web application in a web server running Java 7. Internal mail server destinations failed with "network unreachable", despite telnet and ping working from the same host, and while external mail servers worked. I tried
System.setProperty("java.net.preferIPv4Stack" , "true");
in the code, but that failed. So the parameter value was probably cached earlier by the system. Setting the VM argument
-Djava.net.preferIPv4Stack=true
in the web server startup script worked.
One further bit of evidence: in a very small targeted test program, setting the system property in the code did work. So the parameter is probably cached when the first Socket is used, probably not just as the JVM starts.
Solution 4
well,
I used System.setProperty("java.net.preferIPv4Stack" , "true");
and it works from JAVA, but it doesn't work on JBOSS AS7.
Here is my work around solution,
Add the below line to the end of the file ${JBOSS_HOME}/bin/standalone.conf.bat (just after :JAVA_OPTS_SET
)
set "JAVA_OPTS=%JAVA_OPTS% -Djava.net.preferIPv4Stack=true"
Note: restart JBoss server
Solution 5
you can set the environment variable JAVA_TOOL_OPTS like as follows, which will be picked by JVM for any application.
set JAVA_TOOL_OPTS=-Djava.net.preferIPv4Stack=true
You can set this from the command prompt or set in system environment variables, based on your need. Note that this will reflect into all the java applications that run in your machine, even if it's a java interpreter that you have in a private setup.
Caffeinated Coder
Updated on July 13, 2020Comments
-
Caffeinated Coder almost 4 years
I need to disable IPv6. For that the java documentation indicates setting jvm property
java.net.preferIPv4Stack=true
.But I don't understand how to do it from the code itself.
Many forums demonstrated doing it from the command prompt, but I need to do it at runtime.
-
Caffeinated Coder about 12 yearsThe broadcast address acquired is still 255.255.255.255
-
Chris Dennett about 12 yearsThat seems pretty normal for IPv4. It's the standard 0.0.0.0 network broadcast address.
-
peterh over 10 yearsAre you sure this will work?. From looking at the source it seems to me that
java.net.preferIPv4Stack
is only read when the JVM is started and then never again. Could be wrong though. -
peterh over 10 yearsSorry. Forget that. I was mixing it up with
java.net.preferIPv6Addresses
which is another property. -
leozilla about 8 years@Kal peterh is right, you must set it via command line argument, setting it via System.setProperty inside JVM will not work because it is read on JVM startup
-
Joe23 over 4 yearsThe property is read from a static block in
InetAddress
. That block is executed whenInetAddress
is loaded and not on JVM startup. So if you set the property in a main method before using classInetAddress
, it will work. Not the most reliable thing in the world of course.