I tried setting this variable in my windows environment with Java 7 and doing java -version it gives me it set this variable , as shown as follows
C:\Users\ajduke>set JAVA_TOOL_OPTIONS=-Djava.net.preferIPv4Stack=true -Dfile.e
ncoding=UTF8
C:\Users\ajduke>java -version
Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF
8
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b145)
Java HotSpot(TM) Client VM (build 21.0-b15, mixed mode, sharing)
Answer from ajduke on Stack OverflowI tried setting this variable in my windows environment with Java 7 and doing java -version it gives me it set this variable , as shown as follows
C:\Users\ajduke>set JAVA_TOOL_OPTIONS=-Djava.net.preferIPv4Stack=true -Dfile.e
ncoding=UTF8
C:\Users\ajduke>java -version
Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF
8
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b145)
Java HotSpot(TM) Client VM (build 21.0-b15, mixed mode, sharing)
In case only the first argument is picked up: DON'T USE QUOTES! Just the arguments:
set JAVA_TOOL_OPTIONS=-Xms128m -Xmx512m
In my case (Windows), only the first argument was picked up and reported to be invalid, since I used
set JAVA_TOOL_OPTIONS="-Xms128m -Xmx512m"
and starting any java app results in:
Picked up JAVA_TOOL_OPTIONS: "-Xms128m -Xmx512m"
Invalid initial heap size: -Xms128m -Xmx512m
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Videos
You can use _JAVA_OPTIONS to pass options to any JVM process started on your system.
For example,
set _JAVA_OPTIONS=-Dsun.java2d.noddraw=true
When a JVM starts, it parses the value of _JAVA_OPTIONS as if the parameters were at the command line of java. You can see the passed parameters via JVisualVM.
For more information, read the blog post: What I discovered while trying to pass Default JVM Parameters
And according to https://bugs.openjdk.java.net/browse/JDK-4971166 undocumented Hotspot-specific _JAVA_OPTIONS was superseded by JAVA_TOOL_OPTIONS that is included in standard JVMTI specification, does better handling of quoted spaces and should be always preferred.
Since JDK 9+ there's also JDK_JAVA_OPTIONS as the preferred replacement, see What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?
You have pretty much nailed it except that these options are picked up even if you start JVM in-process via a library call.
The fact that _JAVA_OPTIONS is not documented suggests that it is not recommended to use this variable, and I've actually seen people abuse it by setting it in their ~/.bashrc. However, if you want to get to the bottom of this problem, you can check the source of Oracle HotSpot VM (e.g. in OpenJDK7).
You should also remember that there is no guarantee other VMs have or will continue to have support for undocumented variables.
UPDATE 2015-08-04: To save five minutes for folks coming from search engines, _JAVA_OPTIONS trumps command-line arguments, which in turn trump JAVA_TOOL_OPTIONS.
There is one more difference: _JAVA_OPTIONS is Oracle specific. IBM JVM is using IBM_JAVA_OPTIONS instead. This was probably done to be able to define machine-specific options without collisions. JAVA_TOOL_OPTIONS is recognized by all VMs.