Tuesday, December 17, 2013

What I Discovered while trying to Pass Default JVM Parameters

Hello everyone,
The next post is in fact because of a problem my friend encountered while trying to run the JVisualvm and JConsole on a Windows Platform, That are both *.exe files that come with the JDK, and eventually run a JVM,
Just for some background, he was trying to run them on a Virtual VMWare station,
that for some odd reason was launching the wanted application but showing only a window with a Black background with no actual action that you can do with it, but to close it.

I thought that it might be a problem with the Graphics Card, because the implementation of the JVM tries to draw the GUI via the graphics card by default unless you tell it to do something else.
You can do it by passing the JVM parameter: "-Dsun.java2d.noddraw=true".

(You can read about more Java2D parameters at the next link)
But this is all fine and well but the problem is that there was no way to pass JVM parameters via the command line to *.exe file.

Solution:
The work around to this problem is the default JVM Parameters that you can pass to all the JVM's that will be launched in your machine
(No matter if an executable file like "exe" runs them):
1)      "_JAVA_OPTIONS"
or
2)      "JAVA_TOOL_OPTIONS"
(I saw another option you can read about it in this link - that will work on an IBM JVM, "IBM_JAVA_OPTIONS", but I've tried it only on an Oracle Hotspot JVM – 1.7u45)
And if you really want to go deep, the actual call that parses the JVM parameters in the JVM implementation is in the next link, it's part of the openJDK, in the method "parse_vm_init_args" at line 1734, where you can see that the "JAVA_TOOL_OPTIONS" is being parsed first, and the   "_JAVA_OPTIONS" second.

The usage is pretty simple, you put an environment variable in your OS (No matter if it's windows or Linux) with the wanted value,
 (For our example:  "_JAVA_OPTIONS =-Dsun.java2d.noddraw=true")
and when the JVM launches it will pick up the wanted value, you can see it in the next example when I ran it in the cmd:



What you can see is an environment variable being set to the wanted value, and a java program being run with no JVM parameters, and then the JVM while lunching picking up the wanted value.
While running the same application you can see the passed parameters via the JVisualVM application:



If you'd like to read some more about other kind of JVM parameters that you can pass at startup, you can read the next post in a blog I saw.

I wrote a little java program that let's you list out all the parameters passed to the JVM:


So, for conclusion,
When you'd like to pass default parameters to the JVM without mentioning them literally,
or to pass them to an executable that runs a JVM, you can do it in the methods mentioned above,
by the way, the workaround I mentioned help my friend out J
Hope this help some else,
Demi Ben-Ari.