3

I tried the org.gradle.jvmargs in gradle.properties, but it doesn't work. Specifically, I'm trying to change the GC of the JVM of my program, with Gradle. I don't care about the JVM Gradle runs on, but the one my application does.

  • org.grade.jvmargs should work: see https://stackoverflow.com/questions/40690701/how-to-set-gradle-environment-variables Are you sure you are “doing it right”? – Bohemian Dec 06 '20 at 06:51
  • How does gradle start your Java application? Normally Gradle is used to compile your application e.g. in a jar file. The you would run the application via the command line or the IDE. – Spyros K Dec 06 '20 at 06:54
  • From the Gradle docs https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties it states that: "This does not affect the JVM settings for the Gradle client VM" for the org.grade.jvmargs. –  Dec 06 '20 at 06:56
  • @SpyrosK I'm using Intellij Idea. I run the app with configurations at the top right. –  Dec 06 '20 at 07:04

1 Answers1

4

The Gradle documentation for the Application Plugin says this:

If your application requires a specific set of JVM settings or system properties, you can configure the applicationDefaultJvmArgs property. These JVM arguments are applied to the run task and also considered in the generated start scripts of your distribution.

You should put the GC options into that property.

Note that this won't directly affect the behavior of an executable JAR if you execute it directly; i.e. without using the generated start scripts.


There are two other alternatives (solely!) for the case where you are running the application from Gradle.

  • The JavaExec task type allows you to set the JVM options via various properties; see the JavaExec API documentation

  • The Exec task type allows you to run an arbitrary command with arbitrary arguments. However if you use this to run a Java application, you will need to "wrangle" all of the command line options yourself.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I tried that too, but it had no effect. What exactly you mean by: this won't directly affect the behavior of an executable JAR if you execute it directly? What else can I do? –  Dec 06 '20 at 07:03
  • 1
    *"I tried that too, but it had no effect."* - You need to say clearly what you tried and how you tried it. However, AFAIK, nothing can create an executable JAR file that sets its own JVM options. Executable JARs don't have that capability. You can verify that by reading the JAR file spec. The simple alternative is to use a wrapper script that sets the options. – Stephen C Dec 06 '20 at 07:08
  • I applied the 'application' plugin and added a line: applicationDefaultJvmArgs = ["args", "args", ...], where args various args. All that in the build.gradle file. My IDE can properly specify JVM arguments, so I suppose Gradle can too, right? I know that JAR files have no JVM, but I just want Gradle to add the JVM arguments I want and then run that JAR on that JVM. –  Dec 06 '20 at 07:31
  • The more I think about it, applicationDefaultJvmArgs should work. I don't get why it doesn't. I'm starting to suspect that my IDE somehow overrides the options I'm passing to the JVM via Gradle, I don't know.. –  Dec 06 '20 at 07:54
  • 1
    task runApp(type: JavaExec) did the trick, with allJvmArgs = ["args", "args", ...] –  Dec 06 '20 at 08:04