How to set java.net.preferIPv4Stack=true at runtime?
Asked Answered
A

6

38

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.

Arras answered 27/3, 2012 at 1:51 Comment(4)
Disabling IPv6 to work around some bug you have in your code is just storing up trouble for the future. Fix the real issue now and save yourself a headache later.Myrlmyrle
I'd like to find out why you feel that you need to disable IPv6. There may be a legitimate reason, but I've not seen one yet...Attention
@BrianKnoblauch Here you are: authbind works only with IP4.Rajab
Another reason: Apache Ignite clusters may get partitioned unless this property is set: ignite.apache.org/docs/latest/clustering/network-configurationMasochism
P
58

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

This is equivalent to passing it in the command line via -Djava.net.preferIPv4Stack=true

Poisson answered 27/3, 2012 at 2:2 Comment(6)
The broadcast address acquired is still 255.255.255.255Arras
That seems pretty normal for IPv4. It's the standard 0.0.0.0 network broadcast address.Trossachs
Are 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.Cosentino
Sorry. Forget that. I was mixing it up with java.net.preferIPv6Addresses which is another property.Cosentino
@Poisson 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 startupFotinas
The property is read from a static block in InetAddress. That block is executed when InetAddress is loaded and not on JVM startup. So if you set the property in a main method before using class InetAddress, it will work. Not the most reliable thing in the world of course.Mountie
T
9

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.

Tletski answered 18/7, 2012 at 23:53 Comment(0)
A
9

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.

Ambivert answered 31/3, 2014 at 15:10 Comment(0)
F
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

Feed answered 28/12, 2014 at 7:40 Comment(0)
K
2

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.

Kentigerma answered 12/7, 2016 at 18:15 Comment(0)
P
0

System.setProperty is not working for applets. Because JVM already running before applet start. In this case we use applet parameters like this:

    deployJava.runApplet({
        id: 'MyApplet',
        code: 'com.mkysoft.myapplet.SomeClass',
        archive: 'com.mkysoft.myapplet.jar'
    }, {
        java_version: "1.6*", // Target version
        cache_option: "no",
        cache_archive: "",
        codebase_lookup: true,
        java_arguments: "-Djava.net.preferIPv4Stack=true"
    },
       "1.6" // Minimum version
    );

You can find deployJava.js at https://www.java.com/js/deployJava.js

Planimetry answered 30/9, 2015 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.