Pass a Parameter to All JVMs in A Machine at The Same Time
In this tutorial we are going to see a scenario where you need to change a JVM parameter, but you can’t or is not a good solution to changing the start script of your server(s).
JAVA TOOL OPTIONS
Imagine a scenario where you need to change a JVM parameter, but you can’t or is not a good solution to changing the start script of your server(s).
One of the challenges we had, when we were working with containers, was a way to change a parameter to a JVM without building the docker image again.
The application at the start time should read a JVM parameter where a user profilewas defined. For specific reasons we sometimes need to change this profile, for instance, to use a more controlled user where we can debug an issue. In these situations, we want to stop the container/pod, change the profile and start again, or even start a different container with a different profile.
As you know, the way to pass a JVM parameter to an application is
java -Dprofile=myValue com.some.Application appParameter1 appPaparameter2
What if I don’t have access, or is not viable to rewrite the above line?
You can add to the Environment variables of your operating system
JAVA_TOOL_OPTIONS=‘-Dvar1=value1 -Dvar2=value2’
Yes, you can add multiple values, since it is not a parameter, but an environment variable whose values will be injected into the JVM at starting time.
“ When the JVM starts it search’s for this environment variable, and uses it. You can check the output of the JVM displaying a message with the values found.” |
JDK JAVA OPTIONS
This environment variable has a different purpose, it adds its values as a prefix to the Java other values
Example
export JDK_JAVA_OPTIONS=‘-Dparam=value @someFile
When we execute this:
java @otherFile
The real execution is:
java -Dparam=value @somefile @otherFile