I can't figure out to get this working.
Scenario:
What I want
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
JAVA_HOME
to the desired JDKI could live with many suggestions:
use JAVA_HOME=<my special JDK path defined somewhere else defined>
)Question:
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.
This question is related to
java
gradle
build.gradle
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
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>
As seen in Gradle (Eclipse plugin)
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
File > Project Structure > SDK Location > JDK Location >
/usr/lib/jvm/java-8-openjdk-amd64
GL
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.
I added this line in my GRADLE_HOME/bin/gradle file -
export JAVA_HOME=/path/to/java/version
If you just want to set it once to run a specific command:
JAVA_HOME=/usr/lib/jvm/java-11-oracle/ gw build
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.
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.
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.
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.
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:
-release
compiler argumentI 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
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"
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.
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
Source: Stackoverflow.com