Pass a Parameter to All JVMs in A Machine at The Same Time
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).
Join the DZone community and get the full member experience.
Join For FreeJAVA_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 profile_was 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 isjava -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
Published at DZone with permission of Jose Cruz. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments