Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to set java.net.preferIPv6Addresses = true

Tags:

java

ipv6

I have searched many places of making IPv6 enabled to have a dual-stack machine IPv6. I found many have answered to set java.net.preferIPv6Addresses=true which is set to false by default.

I want to know where exactly should I make these changes, i.e in which file or do I have to write some Java code to put this into.

like image 592
user27 Avatar asked Nov 28 '13 09:11

user27


Video Answer


1 Answers

The setting needs to get into the JVM's system properties ... and it needs to be there before the relevant part of the Java class library initializes.

The "bomb proof" way to do this is to pass the setting to the JVM as a command line parameter; e.g.

    java -Djava.net.preferIPv6Addresses=true ... com.example.MainClass <args>

You could also code your application to inject the setting using

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

but you need to ensure that the injection happens soon enough1, and that would not be trivial.

1 - "Soon enough" means before JVM networking code's static initialization has occurred. This can be difficult to achieve in a complex application. Note the the Network Properties documentation states: "Some are checked only once at startup of the VM, and therefore are best set using the -D option of the java command ...". Note that it does NOT state that those properties can only be set that way.


The suggestion of using a JAVA_OPTS environment variable will only work for some applications. The handling of JAVA_OPTS will happen in your application's launcher or wrapper script before the JVM is launched. The same applies to _JAVA_OPTIONS ... which is one that I've not seen before.

(If the application you are using is properly documented, then its documentation should explain how to specify options that need to be passed to the java.exe launcher.)

The standard java.exe and javaw.exe commands certainly DO NOT pay any attention to the JAVA_OPTS environment variable.

like image 175
Stephen C Avatar answered Oct 30 '22 00:10

Stephen C