How do I add default JVM arguments with Gradle

后端 未结 3 1778
孤独总比滥情好
孤独总比滥情好 2020-12-31 03:55

I need to add default JVM options to my jar, when build with Gradle. From the documentation I got that I have to set:

applicationDefaultJvmArgs = [\"-Djavafx         


        
相关标签:
3条回答
  • 2020-12-31 04:16

    you can use command-line with gradle task:

    class AppRun extends JavaExec {
        private boolean withDebug
    
        @Option(option = "with-debug", description = "enable debug for the process. ")
        public void setDebugMode(boolean debug) {
            this.withDebug = debug
        }
    
        public boolean getDebugMode() {
            return this.withDebug
        }
    }
    
    task run(type: AppRun) {
    }
    

    then run task with options

    gradle run --with-debug

    0 讨论(0)
  • 2020-12-31 04:24

    applicationDefaultJvmArgs is provided by the Application plugin. So if you apply that plugin, the error would probably go away, and you should be able to execute the program by issuing gradle run once you set the mainClassName property to the fully qualified class name, the main method of which you want to invoke.

    0 讨论(0)
  • 2020-12-31 04:36

    From the top of my head I can think of 2 options:

    Option1: Do what @Ethan said, it'll likely work:

    package placeholder;
    
    //your imports
    
    public class Application{
      static {
          System.getProperties().set("javafx.embed.singleThread", "true");  
      }
      // your code
      public static void main(String... args){
        //your code
      }
    }
    

    Option 2: Use the application plugin + default jvm values

    build.gradle:

    apply plugin: 'application'
    //your code
    applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
    

    Now you can run your code 2 ways:

    From gradle

    $gradle run
    

    From distribution(script). from the generated script that the application plugin will provide:

    $gradle clean build distZip
    

    Then gradle will generate a zip file somewhere under ${your.projectdir}/build. Find the zip then unzip it and under /bin you'll find a ${yourproject}.bat and ${yourproject} executables. One is for Linux/mac/unix (${yourproject}) the other one is for windows (${yourproject.bat})

    Option 3 (Android Developer): Use gradle.properties to set jvm argument

    # Project-wide Gradle settings.
    
    # IDE (e.g. Android Studio) users:
    # Gradle settings configured through the IDE *will override*
    # any settings specified in this file.
    
    # For more details on how to configure your build environment visit
    # http://www.gradle.org/docs/current/userguide/build_environment.html
    
    # Specifies the JVM arguments used for the daemon process.
    # The setting is particularly useful for tweaking memory settings.
    # Default value: -Xmx1024m -XX:MaxPermSize=256m
    # org.gradle.jvmargs=-Xmx1024m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 
    
    # You can setup or customize it according to your needs and combined with the above default value.
    org.gradle.jvmargs=-Djavafx.embed.singleThread=true
    

    For more info on how to use gradle build environment on docs.gradle.org

    0 讨论(0)
提交回复
热议问题