[java] How to set java.net.preferIPv4Stack=true at runtime?

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.

This question is related to java ipv6 broadcast ipv4

The answer is


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


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


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.


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.


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to ipv6

What is IPV6 for localhost and 0.0.0.0? Make docker use IPv4 for port binding How to set java.net.preferIPv4Stack=true at runtime? what does "dead beef" mean? How can I convert IPV6 address to IPV4 address? Get IPv4 addresses from Dns.GetHostEntry() Is there a way for non-root processes to bind to "privileged" ports on Linux? How do ports work with IPv6? Maximum length of the textual representation of an IPv6 address? Regular expression that matches valid IPv6 addresses

Examples related to broadcast

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

Examples related to ipv4

What is IPV6 for localhost and 0.0.0.0? How to set java.net.preferIPv4Stack=true at runtime? Validating IPv4 addresses with regexp How can I convert IPV6 address to IPV4 address? What is the total amount of public IPv4 addresses? What is the largest Safe UDP Packet Size on the Internet Get IPv4 addresses from Dns.GetHostEntry() How to convert an IPv4 address into a integer in C#?