Which installed JDK used during Gradle build process - java

This was my output of gradle -v (in a project using the wrapper):
$ ./gradlew -v
------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------
Build time: 2018-11-26 11:48:43 UTC
Revision: 7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987
Kotlin DSL: 1.0.4
Kotlin: 1.3.10
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 11.0.1 (Oracle Corporation 11.0.1+13-LTS)
OS: Linux 3.10.0-862.11.6.el7.x86_64 amd64
See especially this line:
JVM: 11.0.1 (Oracle Corporation 11.0.1+13-LTS)
I was wishing to switch to OpenJDK 11. So select it as you can see below:
# alternatives --config java
There are 4 programs which provide 'java'.
Selection Command
-----------------------------------------------
* 1 /usr/java/jdk-11.0.1/bin/java
+ 2 /usr/local/jdk-11.0.1/bin/java
3 /usr/java/jre1.8.0_191-i586/bin/java
4 /usr/java/jdk1.8.0_191-amd64/jre/bin/java
Enter to keep the current selection[+], or type selection number: 2
# java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
But there is no difference in gradle -v output. So I searched the web and find some ways (see here):
Editing gradle.properties file
Using -Dorg.gradle.java.home command line option
Editing build.gradle file
I used the first two ways. Both worked (to test I switched to JDK 8 and then run build task. The task failed due to some new features in my codes that aren't supported by Java 8). But the result of gradle -v remained unchanged still! Even using the second way:
# ./gradlew -Dorg.gradle.java.home=/usr/java/jdk1.8.0_191-amd64 -v
------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------
Build time: 2018-11-26 11:48:43 UTC
Revision: 7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987
Kotlin DSL: 1.0.4
Kotlin: 1.3.10
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 11.0.1 (Oracle Corporation 11.0.1+13-LTS)
OS: Linux 3.10.0-862.11.6.el7.x86_64 amd64
So the question is how to check which JDK version is used by Gradle during build process?

You can add a task that prints what you need when executed (Kotlin DSL):
tasks {
val j by creating {
doLast {
println(System.getProperty("java.home"))
}
}
}
Groovy DSL:
tasks.register("j") {
doLast {
println System.getProperty("java.home")
}
}
Then executing ./gradlew j:
/usr/lib/jvm/java-8-openjdk/jre
Why could gradlew use another JVM? Take a look at this script and you'll see that it uses JAVA_HOME variable to search for JVM. So probably the version from your PATH is not the same, that JAVA_HOME is pointing to.

I find an alternative way (except #madhead answer) just for when you use a Gradle daemon:
First, find PID of daemon by running gradlew --status (see here for more information). Sample output:
PID STATUS INFO
11432 IDLE 5.0
Only Daemons for the current Gradle version are displayed. See https://docs.gradle.org/5.0/userguide/gradle_daemon.html#sec:status
Then use PID to find which JDK is used:
ll /proc/<PID>/exe
Sample output:
lrwxrwxrwx. 1 0xy 0xy 0 Jan 5 04:03 /proc/11432/exe -> /usr/local/jdk-11.0.1/bin/java
On Windows:
> wmic process where "processId=<PID>" get Name, ProcessID, ExecutablePath
Sample output:
ExecutablePath Name ProcessId
C:\Program Files\Java\openjdk-11.0.1\bin\java.exe java.exe 11432

Related

gradle wrapper reports x86-64 architecture instead of arm-v8 architecture on Mac m1

Hello when working from a Mac m1, Gradle wrapper task reports incorrect architecture compared to gradle :
Here is the result of ./gradlew clean :
operating system 'Mac OS X'
architecture 'x86-64'
osx
x86_64
x86_64
Here is the result of gradle clean :
operating system 'Mac OS X'
architecture 'arm-v8'
osx
aarch_64
aarch64
Does someone have an explanation for that.
Additional information : output of gradle --version
------------------------------------------------------------
Gradle 7.4.2
------------------------------------------------------------
Build time: 2022-03-31 15:25:29 UTC
Revision: 540473b8118064efcc264694cbcaa4b677f61041
Kotlin: 1.5.31
Groovy: 3.0.9
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 18.0.1 (Homebrew 18.0.1+0)
OS: Mac OS X 12.2.1 aarch64
here is the content of my build.gradle to display architecture :
println org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentOperatingSystem;
println org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentArchitecture;
println osdetector.os
println osdetector.arch
println System.getProperty("os.arch")
I generated the Gradle wrapper from my Mac m1 using gradle wrapper.
Thanks to #Jesper's comment here is a solution to that issue.
TL;DR
List the JVMs on your system and verify that you have at lease one (arm64) JVM.
➜ ~ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
18.0.1.1 (arm64) "Oracle Corporation" - "OpenJDK 18.0.1.1" /Users/you/Library/Java/JavaVirtualMachines/openjdk-18.0.1.1/Contents/Home
18.0.1.1 (x86_64) "Oracle Corporation" - "OpenJDK 18.0.1.1" /Users/you/Library/Java/JavaVirtualMachines/openjdk-18.0.1.1-1/Contents/Home
18.0.1 (arm64) "Homebrew" - "OpenJDK 18.0.1" /opt/homebrew/Cellar/openjdk/18.0.1/libexec/openjdk.jdk/Contents/Home
/Users/you/Library/Java/JavaVirtualMachines/openjdk-18.0.1.1/Contents/Home
Setup your JAVA_HOME environment variable to an arm64 JVM :
export JAVA_HOME=`/usr/libexec/java_home -a arm64`
What happened in my case :
This is caused by multiple JVMs installed on the system. Some JVMs are compiled for arm64 and run natively, and some other are compiled for x86_64 and run in a "translator" (rosetta).
Oracle x86_64 JDK installed by intelliJ IDEA.
OpenJDK arm64 installed by Homebrew as a dependency of gradle.
Did not follow the "Caveat" of openJDK :
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
So the homebrew installed gradle was using the homebrew installed JVM. But gradlew scripts were using the default system JVM (the Oracle x86_64 JVM).

Gradle Breaks out of Bash for loop

I have a version history that I'm trying to run a gradle build on and in my bash script I have
while read -r version
do
git checkout $version
gradle clean
gradle javadoc
...
done < version-history.log
which is trying to get the documentation for every tagged release of my code. The problem is that gradle runs a build once and then breaks out of the for loop. I tried encapsulating it in a function call, but it still manages to break out of the loop. Is this behavior intentional? How can I prevent it from doing this? My gradle version is
------------------------------------------------------------
Gradle 6.2.1
------------------------------------------------------------
Build time: 2020-03-01 17:25:20 UTC
Revision: <unknown>
Kotlin: 1.3.61
Groovy: 2.5.8
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 13.0.2 (Oracle Corporation 13.0.2+8)
OS: Linux 5.5.6-arch1-1 amd64
Gradle consumes the input for some reason, which is what causes this behavior. Appending </dev/null to the gradle call seems to fix the problem, try
while read -r version; do
git checkout $version
gradle clean </dev/null
done <version-history.log

How to fix Gradle not recognizing Java 10 on Linux?

When working with the latest Gradle version with Java 10 on Linux, it fails to identify version number of Java as 10 as valid Java version. How to fix this problem on Linux machines ? Log details of the error here
And here is the complete error report (N.B. Running gradle --version or gradle -v also gives the same output :
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine java version from '10'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
EDIT :
Output of gradle --stacktrace
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine java version from '10'.
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
java.lang.IllegalArgumentException: Could not determine java version from '10'.
at org.gradle.api.JavaVersion.toVersion(JavaVersion.java:70)
at org.gradle.api.JavaVersion.current(JavaVersion.java:80)
at org.gradle.internal.jvm.UnsupportedJavaRuntimeException.assertUsingVersion(UnsupportedJavaRuntimeException.java:29)
at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:32)
at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:210)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:174)
at org.gradle.launcher.Main.doAction(Main.java:33)
at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:60)
at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:37)
at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
Output of java --version
java 10 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)
You need to upgrade Gradle. Version 4.5.1 and later definitely support Java 10.
If you think you have 4.5.1 or later installed, but gradle -version still won't run, it's possible that when you type gradle at the command line, it's still running an older version for some reason.
One common thing to do is alias gradle to ./gradlew, so that in the context of a Gradle project, you'll use the wrapper Gradle rather than the system Gradle. This is usually helpful, but in a situation like this, it backfires. You should be able to determine the installed system Gradle with which:
> which gradle
/usr/local/bin/gradle
> $(which gradle) -version
------------------------------------------------------------
Gradle 4.6
------------------------------------------------------------
Build time: 2018-02-28 13:36:36 UTC
Revision: 8fa6ce7945b640e6168488e4417f9bb96e4ab46c
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 10.0.1 ("Oracle Corporation" 10.0.1+10)
OS: Mac OS X 10.13.4 x86_64
(I've elided some warnings about illegal reflective access, if you get those, don't worry about them.)
If $(which gradle) -version still won't run, you need to upgrade the system gradle. But if it does, to update the Gradle wrapper, you can edit your build.gradle --
task wrapper(type: Wrapper) {
gradleVersion = '4.6'
}
-- and then use the system Gradle to rerun the wrapper task:
> $(which gradle) wrapper
Starting a Gradle Daemon (subsequent builds will be faster)
BUILD SUCCESSFUL in 7s
1 actionable task: 1 executed
> ./gradlew -version
Downloading https://services.gradle.org/distributions/gradle-4.6-bin.zip
......................................................................
[etc.]
------------------------------------------------------------
Gradle 4.6
------------------------------------------------------------
Build time: 2018-02-28 13:36:36 UTC
Revision: 8fa6ce7945b640e6168488e4417f9bb96e4ab46c
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 10.0.1 ("Oracle Corporation" 10.0.1+10)
OS: Mac OS X 10.13.4 x86_64
Take help of ppa and upgrade your gradle version , Ubuntu 17 comes with gradle sudo apt install gradle will install default gradle version.
to upgrade for java 10 need to run below command.
sudo add-apt-repository ppa:cwchien/gradle
sudo apt-get update
sudo apt upgrade gradle
Guys I Found the problem in this case. I installed gradle from the official debian repositories which contained an outdated version of gradle which didn't support Java 10. On downloading and installing Gradle from the official site and soft linking it to the /usr/bin/gradle , it worked just fine. So the problem essentially was installing gradle from the debian repositories which are currently outdated.

'android-24' requires JDK 1.8 or later to compile

I use Android Studio and recently got the error:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.
But I have JDK 1.8 installed already:
:\Users..>java -version
java version "1.8.0_91" Java(TM) SE Runtime
Environment (build 1.8.0_91-b15) Java HotSpot(TM) 64-Bit Server VM
(build 25.91-b15, mixed mode)
How to fix it? Thanks
In Android Studio open:
File > Project Structure > see if JDK location points to your JDK 1.8 directory.
Note: you can use compileSdkVersion 24
I have a same problem for days. I found a solution which worked from me on Windows. (I also set environment variables, but don't know whether it's needed.)
Add these lines to build.gradle file:
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
Source: https://code.google.com/p/android/issues/detail?id=203850
I installed latest jdk but it was using the older one until I did this:
app rt-click >> module settings >> sdk location >> (old jdk path)
Replace the old JDK path with your new jdk path.
Works like a charm now :)
Just an FYI I had this error with an Ionic project. All the commands came back pointing to the correct JDK and JAVA_HOME was set correctly.
There was an entry in my PATH to JDK 1.7\bin, replacing that with the 1.8\bin fixed it for me.
I was also running into the same issue from the command line on my Mac, but the answer was that JAVA_HOME was getting overridden. To track down where it is getting overridden first check java from the command line:
$ java -version
java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)
Then check which version of Java that Gradle is using:
$ gradle -version
------------------------------------------------------------
Gradle 2.13
------------------------------------------------------------
Build time: 2016-04-25 04:10:10 UTC
Build number: none
Revision: 3b427b1481e46232107303c90be7b05079b05b1c
Groovy: 2.4.4
Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM: 1.8.0_92 (Oracle Corporation 25.92-b14)
OS: Mac OS X 10.11.5 x86_64
At this point, I still hadn't found the source of the problem. Did a little digging, and found that it was indeed overridden in my gradle.properties file even though it was showing Java 8 when I executed gradle -version:
org.gradle.daemon=true
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
org.gradle.jvmargs=-XX:MaxPermSize=512m -XX:-UseSplitVerifier -Xms512m -Xmx6144m
To fix, I just deleted the java.home line from gradle.properties. Hopefully this helps if anyone else is running into the same issue from the command line.
In Android Studio open:
File > Project Structure > SDK Location > Select jdk1.8.0_101.jdk > Home
I was also running into the same issue from the command line on my Windows, it shows the same hint.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. `compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.`
check JDK location in Project Structure.
In Android Studio open: File > Project Structure > see if JDK location
points to your JDK 1.8 directory.
It should look like
JDK Location
C:\Program Files\Java\jdk1.8.0_91
I already set the correct location to JDK.
check the java version
D:\AndroidSelfTrainingProject\CustomBuildIdDemo>java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
check the gradle version
.D:\AndroidSelfTrainingProject\CustomBuildIdDemo>gradle -v
------------------------------------------------------------
Gradle 2.10
------------------------------------------------------------
Build time: 2015-12-21 21:15:04 UTC
Build number: none
Revision: 276bdcded730f53aa8c11b479986aafa58e124a6
Groovy: 2.4.4
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.7.0_79 (Oracle Corporation 24.79-b02)
OS: Windows 7 6.1 amd64
JVM version should be 1.8.
To fix it, we can add in the gradle.property
In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory. As for me, it is like:
org.gradle.java.home=C:/Program Files/Java/jdk1.8.0_91
To verify if it is set right, we can make a gradle build task.
D:\AndroidSelfTrainingProject\CustomBuildIdDemo>gradle build
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon
: https://docs.gradle.org/2.10/userguide/gradle_daemon.html.
To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
On Windows 10, I first checked Project Structure and it pointed to an old 1.7 jdk. I closed Android Studio then renamed the old jdks (and jre libraries for consistency) leaving the newest 1.8 library alone. When I restarted Android Studio and attempted to build the application it asked me for the Java JDK library and I gave the path to the newest 1.8 JDK library. On my system it was C:\Program Files\Java\jdk1.8.0_112.

Gradle project compiler missing classes presumably from tools.jar

This one is completely beyond me. :-/
Will take any trouble shooting tips I can get.
* What went wrong:
Execution failed for task ':commons:stuf-widgets:compileJava'.
> com/sun/tools/javac/util/Log$PrefixKind
The above resulted from:
gradle clean;gradle build
Fix I tried:
compile files("${System.properties['java.home']}/../lib/tools.jar")
Got that from another question wherein a class that should have been in tools.jar was not loaded. Adding it changed nothing. I don't see anything special about the dependencies. Using --stacktrace --debug --info did not provide any additional clues other than as noted above. There is nothing special going on like JAXB or WSDL generation.
Environment:
me#mybox-me ~ $ java -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)
me#mybox-me ~ $ which gradle
/####/#######/gradle/bin/gradle
me#mybox-me ~ $ which groovy
/####/#######/groovy/bin/groovy
gradle --version
------------------------------------------------------------
Gradle 2.8
------------------------------------------------------------
Build time: 2015-10-20 03:46:36 UTC
Build number: none
Revision: b463d7980c40d44c4657dc80025275b84a29e31f
Groovy: 2.4.4
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_66 (Oracle Corporation 25.66-b17)
OS: Linux 3.16.0-53-generic amd64
groovy -version
Groovy Version: 2.4.5 JVM: 1.8.0_66 Vendor: Oracle Corporation OS: Linux
Tho me a bone??

Categories

Resources