How do I pass JVM args dynamically at runtime?

Right now I’m using the application plugin with the following to set JVM args:

application {
    applicationDefaultJvmArgs = ['-XX:MaxRAMPercentage=75.0']

The obvious problem with this approach is that the JVM args are fixed once the app is built. How do I pass JVM args dynamically after the app is built?


If you look at the generated start scripts, there are two environment variables that are considered. JAVA_OPTS, and <your application>_OPTS. Both can be used to supply additional JVM parameters at runtime.

You can also rename <applicationName>_OPTS by setting the optsEnvironmentVar property of the startScripts task.

For example,

startScripts {
    optsEnvironmentVar = 'MY_OPTS_ENV_VAR'