How do I tell Gradle to use specific JDK version?

前端 未结 17 2176
时光取名叫无心
时光取名叫无心 2020-11-22 12:12

I can\'t figure out to get this working.

Scenario:

  • I have an application built with gradle
  • The application uses JavaFX

What I w

相关标签:
17条回答
  • 2020-11-22 12:32

    If you are executing using gradle wrapper, you can run the command with JDK path like following

    ./gradlew -Dorg.gradle.java.home=/jdk_path_directory

    0 讨论(0)
  • 2020-11-22 12:36

    I am using Gradle 4.2 . Default JDK is Java 9. In early day of Java 9, Gradle 4.2 run on JDK 8 correctly (not JDK 9).

    I set JDK manually like this, in file %GRADLE_HOME%\bin\gradle.bat:

    @if "%DEBUG%" == "" @echo off
    @rem ##########################################################################
    @rem
    @rem  Gradle startup script for Windows
    @rem
    @rem ##########################################################################
    
    @rem Set local scope for the variables with windows NT shell
    if "%OS%"=="Windows_NT" setlocal
    
    set DIRNAME=%~dp0
    if "%DIRNAME%" == "" set DIRNAME=.
    set APP_BASE_NAME=%~n0
    set APP_HOME=%DIRNAME%..
    
    @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
    set DEFAULT_JVM_OPTS=
    
    @rem Find java.exe
    if defined JAVA_HOME goto findJavaFromJavaHome
    
    @rem VyDN-start.
    set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_144\
    @rem VyDN-end.
    
    set JAVA_EXE=java.exe
    %JAVA_EXE% -version >NUL 2>&1
    if "%ERRORLEVEL%" == "0" goto init
    
    echo.
    echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
    echo.
    echo Please set the JAVA_HOME variable in your environment to match the
    echo location of your Java installation.
    
    goto fail
    
    :findJavaFromJavaHome
    set JAVA_HOME=%JAVA_HOME:"=%
    
    
    @rem VyDN-start.
    set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_144\
    @rem VyDN-end.
    
    
    set JAVA_EXE=%JAVA_HOME%/bin/java.exe
    
    if exist "%JAVA_EXE%" goto init
    
    echo.
    echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
    echo.
    echo Please set the JAVA_HOME variable in your environment to match the
    echo location of your Java installation.
    
    goto fail
    
    :init
    @rem Get command-line arguments, handling Windows variants
    
    if not "%OS%" == "Windows_NT" goto win9xME_args
    
    :win9xME_args
    @rem Slurp the command line arguments.
    set CMD_LINE_ARGS=
    set _SKIP=2
    
    :win9xME_args_slurp
    if "x%~1" == "x" goto execute
    
    set CMD_LINE_ARGS=%*
    
    :execute
    @rem Setup the command line
    
    set CLASSPATH=%APP_HOME%\lib\gradle-launcher-4.2.jar
    
    @rem Execute Gradle
    "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.launcher.GradleMain %CMD_LINE_ARGS%
    
    :end
    @rem End local scope for the variables with windows NT shell
    if "%ERRORLEVEL%"=="0" goto mainEnd
    
    :fail
    rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
    rem the _cmd.exe /c_ return code!
    if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
    exit /b 1
    
    :mainEnd
    if "%OS%"=="Windows_NT" endlocal
    
    :omega
    
    0 讨论(0)
  • 2020-11-22 12:37

    If you are using JDK 9+, you can do this:

    java {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    
    tasks.withType<JavaCompile> {
        options.compilerArgs.addAll(arrayOf("--release", "8"))
    }
    

    You can also see the following related issues:

    • Gradle: [Java 9] Add convenience method for -release compiler argument
    • Eclipse Plug-ins for Gradle: JDK API compatibility should match the sourceCompatibility option.
    0 讨论(0)
  • 2020-11-22 12:37

    If you are using Kotlin DSL, then in build.gradle.kts add:

    tasks.withType<JavaCompile> {
        options.isFork = true
        options.forkOptions.javaHome = File("C:\\bin\\jdk-13.0.1\\")
    }
    

    Of course, I'm assuming that you have Windows OS and javac compiler is in path C:\bin\jdk-13.0.1\bin\javac. For Linux OS will be similarly.

    0 讨论(0)
  • 2020-11-22 12:39

    There is one more option to follow. In your gradle tasks available in Eclipse, you can set your desired jdk path. (I know this is a while since the question was posted. This answer can help someone.)

    Right click on the deploy or any other task and select "Open Gradle Run Configuration..."

    Then navigate to "Java Home" and paste your desired java path.

    Please note that, bin will be added by the gradle task itself. So don't add the "bin" to the path.

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