ChatGPT解决这个技术问题 Extra ChatGPT

How do I tell Gradle to use specific JDK version?

I can't figure out to get this working.

Scenario:

I have an application built with gradle

The application uses JavaFX

What I want

Use a variable (defined per developer machine) which points to an installation of a JDK which will be used for building the whole application / tests / ...

I thought about having the gradle.properties file, defining the variable. Something like

JAVA_HOME_FOR_MY_PROJECT=<path to my desired JDK>

What I don't want

point JAVA_HOME to the desired JDK

I could live with many suggestions:

a solution that defines a system environment variable which I'm able to check in my build.gradle script

a variable defined in gradle.properties

overriding the JAVA_HOME variable only for the build context (something like use JAVA_HOME=)

something else I didn't think about

Question:

How to wire a variable (how ever defined, as variable in the gradle.properties, system environment variable, ...) to the build process?

I have more than one JDK7 available and need to point to a special version (minimum JDK_u version).

Any answer is appreciated and I'm thankful for every hint to the right direction.

Have you tried setting org.gradle.java.home in the gradle.properties file? link
in eclipse you can create "product config file" in to your project and you can pack the jdk with your product. No need to specify env variables.
@RayStojonic: I just gave it a try, but gradle still uses the JAVA_HOME JDK for building :(
@mdanaci: I was really hoping I could avoid this because I honestly don't want to take a JDK under version control (which would be the consequence if I would use your suggestion) as any developer has to be able to pack the application. Plus, I see problems with the CI server this way. Thanks for your suggestion anyway :)
As of a year ago, the org.gradle.java.home setting applies only to gradle daemon, apparently... At any rate, try setting fork to true and forkOptions.executable to the jdk you want to use: link

J
Jesse Barnum

Two ways

In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory

or:

In your build.gradle compileJava.options.fork = true compileJava.options.forkOptions.executable = '/path_to_javac'


The gradle.properties can be defined at project level too, see gradle.org/docs/current/userguide/build_environment.html
If you are executing using gradle wrapper you can also do it like ./gradlew -Dorg.gradle.java.home=/path_to_jdk_directory. Good if you don't want to touch gradle.properties.
The question was how to set VERSION, not JDK PATH, wasn't it?
This way I can't share project with other developers, who have java in different paths.
There's a difference between options 1 and 2 that should be clarified: In option 1 we are setting the JVM for gradle itself to run under which will also be used to run the compiler task (notice that javac itself is a java application), while in option 2 we are just setting javac to be spawned in its own JVM. If for whatever reason we need a specific JVM for gradle (e.g a given gradle plugin is compiled for say Java 8), then we would be forced to resort to forking a separate JVM for the compile process.
m
mirmdasif

If you add JDK_PATH in gradle.properties your build become dependent on on that particular path. Instead Run gradle task with following command line parametemer

gradle build -Dorg.gradle.java.home=/JDK_PATH

This way your build is not dependent on some concrete path.


In Eclipse (Neon, 4.6) you can also set the Java Home within the Gradle build configuration (see tab "Java Home"). This is somewhat tedious work if you have 20+ build jobs... I think Gradle really needs to pick the Java Home directory from the system's configuration!
This is a fair point. I have a soft-link called /usr/lib/java/jdk/home that points to the current version installed. Of course when you want a specific version (e.g. u51) then you need to be specific about the path. Also some tools want to kick-off gradle don't seem to set the JDK in the environment they give gradle. And I for one never set the JDK as the current JAVA_HOME unless it is a development session.
Note that JDK_PATH can't have spaces on Windows, even if it's in quotes: you have change "Program Files" to PROGRA~1 (all caps) or whatever else DIR /X tells you.
If you are using gradle wrapper invoke the command like this ./gradlew build -Dorg.gradle.java.home=/JDK_PATH Replace JDK_PATH with your jdk location in your environment like ' /opt/java/jdk1.7.0_79/bin/'
"... gradle itself (either standalone distribution or wrapper) uses JDK from JAVA_HOME environment variable or (if it is not set) from PATH". I specified the JAVA_HOME prior to gradlew call to fix this. (ie. env JAVA_HOME=/path/to/java gradlew war --stacktrace)
G
Geert

To people ending up here when searching for the Gradle equivalent of the Maven property maven.compiler.source (or <source>1.8</source>):

In build.gradle you can achieve this with

apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8

See the Gradle documentation on this.


@morgwai -- except that this isn't the answer to the question, which explicitly asks "What I want [is to set] a variable (defined per developer machine) which points to an installation of a JDK which will be used for building the whole application". The upvoted answers are correct; this answer is for an entirely different question.
This is ignored by tools such as the Javadoc task (which tries to generate the javadoc according to Java 9 with modules on my project that is specified to use Java 8)
While not an exact answer for this question, this way actually works for more than just one system. Most people searching this question want to see this answer and not the other answers.
I asked Google for "gradle specify jdk version" and it brought me here. The answer I was actually looking for is this one, regardless of what the question was actually about. Upvoted because useful.
"except that this isn't the answer to the question", The question actually asks two questions... one is "specific JDK version?" (by title), the other is "an installation of a JDK" (by description). In my experience, the former is often more useful as newer JDKs can often target older JDK versions, allowing the existing location to achieve what the title (perhaps accidentally) asks.
R
Raman

Gradle 6.7+ — Use Gradle Toolchain Support

The right way to do this with modern versions of Gradle (version 6.7+) is to use the Gradle Java Toolchain support.

The following block, when the java plugin is applied to the current project, will use Java 11 in all java compilation, test, and javadoc tasks:

java {
  toolchain {
    languageVersion.set(JavaLanguageVersion.of(11))
  }
}

This can also be set for individual tasks.

NOTE: For other tasks relying on a Java executable or Java home, use the compiler metadata to set the appropriate options. See below for an example with the Kotlin plugin, prior to version 1.5.30.

Gradle attempts to auto-detect the location of the specified JDK via several common mechanisms. Custom locations can be configured if the auto-detection isn't sufficient.

Kotlin 1.5.30+

For Kotlin 1.5.30+, the Kotlin plugin supports toolchains directly:

kotlin {
  jvmToolchain {
    (this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of("11"))
  }
}

Kotlin Earlier Versions

Configuring the Kotlin compiler for versions prior to 1.5.30 involves using the toolchain API to determine the compiler, and passing that information to the plugin. Other compiler-based plugins may be configured in similar ways.

val compiler = javaToolchains.compilerFor {
  languageVersion.set(JavaLanguageVersion.of(11))
}

tasks.withType<KotlinJvmCompile>().configureEach {
  kotlinOptions.jdkHome = compiler.get().metadata.installationPath.asFile.absolutePath
}

Do i need to remove sourceCompatibility & targetCompatibility? Also if i want it to run with a specific java update, let's say Java 8 Update 281 instead of Java 8 Update 271, is it possible?
@jumping_monkey You can't define them at the java { } extension level, but you can still define them at the JavaCompile task level. Or use the new release flag on CompileOptions. See docs.gradle.org/current/userguide/…. About the specific java update, see github.com/gradle/gradle/issues/16628.
how can i use the toolchain for all the plugins? cc @Raman
@SouravJha Depends on the plugin, but my answer describes how to use compiler metadata to set the appropriate options for your plugin(s).
I meant that for my project using Gradle as my build tool, I have the Gradle wrapper, on running the Gradle commands the plugins I have used, should use toolchain java instead of my machine's default java. Is there a global way to set compiler for all the plugins.
J
Jafar Karuthedath

If you have this problem from Intellij IDE, try this options

Set Gradle JVM Home Set the JDK version in the Project module settings Check the JDK version in the Modules


Does this solution still work if you open a terminal and run gradle build? Or is this solution only used when using the IDE to run/compile code?
This is for the IDE
@Johann yes. This can work from some terminal uses of gradle: stackoverflow.com/questions/67079327/…
V
Vittal Pai

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


Y
Yekta Sarıoğlu

You could easily point your desired Java version with specifying in the project level gradle.properties. This would effect the current project rather than altering the language level for every project throughout the system.

org.gradle.java.home=<YOUR_JDK_PATH>

didnt work. Value 'C:Program FilesJavajdk-11.0.8' given for org.gradle.java.home Gradle property is invalid (Java home supplied is invalid) Please provide examples.
escape the backslashes like so org.gradle.java.home=C:\\Program Files\\Java\\jdk-11.0.8
m
m0j0hn

If you are using linux and gradle wrapper you can use following solution.

Add path to local.properties file:

javaHome=<path to JDK>

Add to your gradlew script file:

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source $DIR/local.properties 2>/dev/null

if ! [ -z "$javaHome" ]
then
  JAVA_HOME=$javaHome
fi

In this solution, each developer can set his own JDK path. File local.properties shouldn't be included in version control system.


Bumped into the same problem as OP (being forced to meddle with old project whom the original developer has left), this is the best solution for me so far
Using another environment variable instead of writing it in the local.properties file can also do the same job.
R
Rosberg Linhares

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.


Question: why do wee need to set --release if we are already setting the target?
You saved my life.
K
Kiran

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..."

https://i.stack.imgur.com/AAAQn.png

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

https://i.stack.imgur.com/CoLUP.png

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


Don't see gradle tasks
Is this for Eclipse? If yes, please specify it in your answer.
M
Manoj

I added this line in my GRADLE_HOME/bin/gradle file - export JAVA_HOME=/path/to/java/version


S
Sergey Alekhin

For windows run gradle task with jdk 11 path parameter in quotes

gradlew clean build -Dorg.gradle.java.home="c:/Program Files/Java/jdk-11"

C
Chinese Cat

For Windows, open cmd and enter to your project root, then execute a command like this:

gradlew build -Dorg.gradle.java.home="jdk_path"

My JDK is located in this path: C:\Program Files\Java\jdk-11.0.5.

So, for my case, it looks like this below:

gradlew build -Dorg.gradle.java.home="C:\Program Files\Java\jdk-11.0.5"

Additionally, if you want to specify a certain variant, you can do like this :

gradlew assembleDebug -Dorg.gradle.java.home="E:\AndroidStudio\jre"

This command will compile your project with Debug variant and output 2 apks(debug + release) in 2 folders.


R
Remo Meier

there is a Gradle plugin that download/bootstraps a JDK automatically:

https://plugins.gradle.org/plugin/com.github.rmee.jdk-bootstrap

No IDE integration yet and a decent shell required on Windows.


Where "decent shell" means bash, basically. I just submitted a PR to make it work for the batch file too.
b
btm me

*gradle.properties
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
org.gradle.java.home=C\:\\Program Files\\Java\\jdk-11.0.15
-------------------------------------------------------------------
**build.gradle**
android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion
    compileOptions {
        sourceCompatibility 11
        targetCompatibility 11
    }
    kotlinOptions {
        jvmTarget = '11'
        useIR = true
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }*

after flutter3.0 & jdk11 install these steps worked for me


Old question with the updated answer. that's great +1
P
Paul Verest

As seen in Gradle (Eclipse plugin)

https://i.stack.imgur.com/mwzaD.png

http://www.gradle.org/get-started

Gradle uses whichever JDK it finds in your path (to check, use java -version). Alternatively, you can set the JAVA_HOME environment variable to point to the install directory of the desired JDK.

If you are using this Eclipse plugin or Enide Studio 2014, alternative JAVA_HOME to use (set in Preferences) will be in version 0.15, see http://www.nodeclipse.org/history


Hi Paul, actually, exactly this is my problem. I want to define a different JDK for gradle-building tasks than defined in JAVA_HOME. Though, it's good to know that there will be an entry in the eclipse plugin for this :)
Standard Eclipse allows to define environment variables per launch configuration. But that is not what we try with gradle. Open an issue if you explore this question further github.com/Nodeclipse/nodeclipse-1/issues
T
TacB0sS

So, I use IntelliJ for my Android project, and the following solved the issue in the IDE:

just cause it might save someone the few hours I wasted... IntelliJ -> Preferences -> Build, Execution, Deployment -> Build tools -> Maven -> Gradle

and set Gradle JVM to 1.8 make sure you also have JDK 8 installed...

NOTE: the project was compiling just fine from the command line


l
logbasex

There is a pretty simple way. You can try the following solution.

sudo update-alternatives --config java

After updating your java version, let check Gradle current JVM's version to ensure this change was applied.

gradle -v

For more details, pls review your gradlew file (in Unix-like OS) or gradlew.bat file (in Window OS) to see how Gradle config JAVACMD variable.

For example

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
        # IBM's JDK on AIX uses strange locations for the executables
        JAVACMD="$JAVA_HOME/jre/sh/java"
    else
        JAVACMD="$JAVA_HOME/bin/java"
    fi
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD="java"
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

J
JasonBodnar

If you just want to set it once to run a specific command:

JAVA_HOME=/usr/lib/jvm/java-11-oracle/ gw build

this doesn't work for me on Windows cmd.exe
B
Braian Coronel

Android Studio

File > Project Structure > SDK Location > JDK Location >

/usr/lib/jvm/java-8-openjdk-amd64

GL

Install JDK


s
sobczak.dev

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.


J
James Graham

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

I
IamDOM

Thanks to this answer I was able to identify that my issue was gradle picking the wrong JDK even if the JAVA_HOME was set correctly.

My original comment:

My OS is Linux and I have my JAVA_HOME pointing to the correct path. I have even used this command gradle build -Dorg.gradle.java.home=$JAVA_HOME to prove my point. I've installed gradle using snap in Ubuntu and I'm guessing it is using some bundled JDK.

My solution:

I've removed gradle using snap and then I've manually installed gradle by downloading their binaries and put it in the PATH.

My conclusion is that the gradle snap installation on Ubuntu comes bundled with some older JDK.