Intellij, target JRE vesion doesn't match project jdk version - java

I am using Intellij Community version 2019.3 on my MacBook. I have a simple springboot (java) project. Whenever I run my unit test I get the following warning:
Where should I check on Intellij to get rid of this error? (I heard on internet "Project Settings", but I can't find where this option is)

The target JRE version for Gradle can be configured from within Intellij via the Gradle JVM setting at Preferences (cmd + ,) > Build, Execution, Deployment > Gradle:
The project JDK version can be configured from the Project SDK setting in Project Structure (cmd + ;):
(Screenshots taken from IntelliJ IDEA 2020.1.4)

This is because you are building for target version of Java 1.8
Depending what you are using, Maven or Gradle, you set the target version of java with:
Gradle
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Docs:
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java-extension
https://docs.gradle.org/current/javadoc/org/gradle/api/JavaVersion.html
Maven
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Docs:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
You can change this version to 12 or JavaVersion.VERSION_12 to use your build JDK, but then you would lose the compatibility and won't be able to run the application jar with Java 1.8.
You can also change the build SDK version, on macOS press command + ; and go to Project Settings -> Project and change the Project SDK to the one you want to build it with.

Intellij + Gradle also refers to the Gradle JVM setup within Intellij.
Ensure to change that as shown below:

Related

How can i change the Gradle Version in InteliJ?

I have the Error: "Found invalid Gradle JVM configuration"
"JDK 17.0.1 isn´t compatible with gradle 7.1. Please fix JAVA_HOME enviroment variable"
Im not sure why it says gradle 7.1. The gradle Version i have is 7.4.2. This version should work with JDK 17
Take a look at the gradle-wrapper.properties file in the ./gradle/wrapper directory.
In the file, you can define the distribution URL from which Gradle is loaded. For example:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
As far as I know, IntelliJ adopts the version.
According to the Gradle documentation, Gradle 7.3 is the first Gradle version that supports JDK 17. You could try that one.

General error during semantic analysis: Unsupported class file major version 57

I'm trying to setup libgdx for a desktop game and when I try to generate the project I have this error and the build fails.
I've the latest versions of Java(13.0.2) and Gradle(6.6), both set as environment variables in the path. Can somebody help me?
The error looks like this:
Could not compile settings file 'C:\Users\noemi\Desktop\Test\settings.gradle'.
> startup failed:
General error during semantic analysis: Unsupported class file major version 57
java.lang.IllegalArgumentException: Unsupported class file major version 57
This is not a LibGDX issue. Gradle 5 is incompatible with Java 13. You either need to update Gradle (the wrapped version in your project) to Gradle 6 or later, or you need to use a lower version of the JRE.
To update the wrapped gradle, go to gradle/wrapper/gradle-wrapper-properties in your project and update the version number. I'm using 6.1.1.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
If you have an older LibGDX project, you might have to modify the build.gradle file in the android module to be compatible with Gradle 6. You can copy-paste to replace the copyAndroidNatives task with the one here.
startup failed:
General error during semantic analysis: Unsupported class file major version 61
how to fix this react native issues
JAVA JDK 17
Go to the android/ directory of your react-native project
Create a file called local.properties with this line:
sdk.dir=C:\Android\sdk
GOTO
android\gradle\wrapper\gradle-wrapper.properties
CHANGE
distributionUrl=https://services.gradle.org/distributions/gradle-6.7-all.zip
TO
distributionUrl=https://services.gradle.org/distributions/gradle-7.3-all.zip
GOTO
android\gradle.properties
add this line
org.gradle.jvmargs=-Xmx1536M --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED
This problem occurred when I run my old Flutter project that I coded 2 years ago. I think with Android project will also fix in the same way. I'm use MacOS and Android Studio Bumblebee.
First, need to move to the android folder and check gradle version:
cd android
./gradlew --version
It show Gradle version 6.7.1 and you need attention to the JVM line, and my JDK version is 15.0.2. It is too new for this project. So you must to downgrading from open jdk version to old version or upgrade Gradle version in gradle/wrapper/gradle-wrapper.properties to new version.
If you downgrade JDK version, you can download from this. In my case, I downgraded from Java SE 15 to Java SE 11.
After I open Android of Flutter project
Go to Preferences->Build, Execution, Deployment → Build Tools → Gradle → and choose JDK version 11.
Go to Build -> Clean Project -> Rebuild Project.
Go to Terminal run java --version, if it still show JE version 15 then You must set JAVA_HOME before.
You only need to add the following to your .bash_profile or .zshrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.15.jdk/Contents/Home
Save and go to android folder and run again
./gradlew --version
It will show JDK version 11 correct.
Finally, run flutter run.
If you're using eclipse, go to
Eclipse > Preferences > Gradle
Update Java home to point to Java home location
I have downgraded gradle file thats why this error happens so finally I have to change Gradle wrapper file From
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
Check gradle version and bundle version using the following link
Link https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
If you want to upgrade android/gradle/build.gradle change dependencies classpath 'com.android.tools.build:gradle:[latest version]'

Gradle build project with specific jdk

I am having Grails project (grails 2.3.11) with multiple Grails plugins. One main Grails project in addition of some user defined Grails plugins and core Java project creates a complete application. So far, these projects are build using Ant builder. Recently upgraded build process to use Gradle.
Problem with gradle process is that some project needs to be build with jdk 1.7 and some to be build with jdk 1.8. In order to fix it, I have set java_home to point to jdk 1.7. Then created gradle.properties file in project directories which needs code to be build with jdk 1.8. Have set property org.gradle.java.home to point to jdk 1.8. Now when I print java_home value from such project I found that it is still picking jdk 1.7 (the one set as environment variable).
Here my gradle compileJava task:
compileJava {
options.encoding = 'UTF-8'
def env = System.getenv("JAVA_HOME")
println "Compile Java: JAVAHOME = ${env}"
dependsOn ':core:updateversiondotjava'
logging.captureStandardOutput LogLevel.DEBUG
}
Projects build with 1.7 are all Grails project and Grails user defined plugins. Reason to build them with 1.7 is to avoid grails version upgrade. Because grails 2.3.x or lower are not supporting Java 1.8.
Can anyone please guide me where I am doing wrong?
I recall Grails 2.3.11 running on Java 8 in my testing. Early versions of Grails 2.3 did not run on Java 8. I understand the official support started with 2.4.x but have you tried Java 8 with your 2.3.11 application?

Error:java: invalid source release: 8 in Intellij. What does it mean?

Im trying to compile some code in I'm using Intellij Ultimate 13.1.4, but I get the following error and I have no idea what it means:
Information:Using javac 1.7.0_55 to compile java sources
Information:java: Errors occurred while compiling module 'Example'
Information:Compilation completed with 1 error and 0 warnings in 3 sec
Information:1 error
Information:0 warnings
Error:java: invalid source release: 8
My guess is that its something related to Java 8 vs Java 7, but I have no idea what specifically. I've tried to Google around for this message, but they either talk about javac or target release, so it doesn't exactly seem to apply.
I had the same issue when "downgrading" a project from Java 8 to Java 6. The reason was that it was not changed at all places in IntelliJ.
In IntelliJ 13.1.4 I had to change Java and SDK version on the following places not to get this error:
File -> Project Structure -> Project Settings
File ->
Project Structure -> Module Settings -> Tab: Sources: Language Level
File -> Project
Structure -> Module Settings -> Tab: Dependencies: Module SDK
File -> Settings -> Compiler -> Java Compiler -> Target bytecode
version
The last bullet was the one that was not updated in my case. Once I changed this, the error disappeared.
Check your pom.xml first (if you have one)
Check your module's JDK dependancy. Make sure that it is 1.8
To do this,go to Project Structure -> SDK's
Add the path to where you have stored 1.8 (jdk1.8.0_45.jdk in my case)
Apply the changes
Now, go to Project Structure ->Modules
Change the Module SDK to 1.8
Apply the changes
Voila! You're done
For Gradle users having this issues, if nothing above helps this is what solved my problem - apply this declarations in your build.gradle files:
targetCompatibility = 1.6 //or 1.7;1.8 and so on
sourceCompatibility = 1.6 //or 1.7;1.8 and so on
Problem solved!
Change in pom.xml 1.6 to 1.8
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
If you are using Gradle as a build tool and you get this error when executing a Gradle task i.e TomcatRun take a look to my other answer to the same question
javac: invalid target release: 1.8
It can be simply overcome by setting on Project Structure. You just need to select the right path for related version of JDK. Select new on dependencies tab, and choose the path. It's done!
Andreas Lundgren's answer worked and I was able to compile and run my app.
However, when I tried to run the project's associated JUnit tests I received the same error. Running
gradle -version
from Windows command prompt showed that gradle was still picking up the incorrect jdk. To fix it I had to set the JAVA_HOME environment variable to point to the correct jdk and restart IntelliJ.
I checked all above said project version, module version, project bytecode version, target bytecode version settings in IntelliJ Idea, but all were the same as I scratched.
I face this error Error:java: invalid source release: 1.8 in IntelliJ Idea 2017.2.6 because I upgraded the dependency version Maven pom file, which(dependency) were supposed to build for JDK 1.8 application and I were building my application on and with maven compiler source and target JDK 1.7.
Hence I again downgrade the dependency version to earlier one in Maven pom, and the error gone after project Rebuild Module 'xyz_project'.
If You Get This ERROR: Error:java: invalid source release: 12
Information:java: Errors occurred while compiling module 'IdeaProjects'
Information:javac 1.8.0_211 was used to compile java sources
Information:Module "IdeaProjects" was fully rebuilt due to project configuration/dependencies changes
Information:7/12/2021 8:21 PM - Build completed with 1 error and 0 warnings in 6 s 323 ms
Error:java: invalid source release: 12
THEN
Select Your Previous Version Project
In My side :
FINISHED
If you Get SOlution of Your IDE then Message Me www.betechnical.tech
or amankushwaha9918#gmaill.com
For Grails users, apply these declarations in your BuildConfig.groovy file:
grails.project.target.level = 1.6 //or 1.7;1.8 and so on
grails.project.source.level = 1.6 //or 1.7;1.8 and so on
You need to click to the project
Open Module Settings and change the path of your JDK, if in the file POM you use jdk 1.8, configure jdk 1.8 with correct path.
I add one more path unmentioned in this answer https://stackoverflow.com/a/26009627/4609353
but very important is Edit Configurations
I tried out all the steps mentioned in here https://stackoverflow.com/a/26009627/2058104, but the 4th point has now changed. You need to go to Preferences -> Build, Execution, Deployment -> Compiler -> Java Compiler
In there, as shown in below figure, you need to change the "Target bytecode version". Although, I changed it to 8 (since I needed to downgrade to Java 8), it was giving the same error, over and over. Therefore, try to remove the existing entry (in this table) and add it again. This worked for me.
On the other hand, clean the project and try to run again.
I had the same issue the solution for me was to change my java version in the pom.xml file.
I changed it from 11 to 8.
Lots of good answers.
For those using the (almost) latest version of Intellij, at the time of writing, what can be said, is that the project JDK can be at a higher level, than that of the module.
In fact without it, Maven will have to be rolled back to an older version.
Therefore with the following version of Intellij:
One should not change the project level JDK and therefore be able to leverage the Maven or Gradle settings when building, but when running Maven or running Gradle using a more modern version of the JDK.
If you lower your project level JDK from say JKD8 to JDK6, Maven or Gradle will not run.
Keeping your module at a lower level JDK-wise will enable you to build it to that version, if you use the Module rebuild or build options; using the menu options for rebuilding the project will complain wit "Invalid source release:8...".
As Andreas mentioned all about:
Error:java: invalid source release: 8 in IntelliJ
Error:java: invalid source release: 13 in IntelliJ
Error:java: invalid source release: 14 in IntelliJ...
OR whatever version you are using in Java...
The problem will exist if you do not have it matching inside the below code:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
This 1.8 in my case, must be matching on your device through MAVEN project library, settings, preferences, project setting and SDK.
I was recently facing the same problem.
This Error was showing on my screen after running my project main file.
Error:java: invalid source release: 11
Follow the steps to resolve this error
File->Project Structure -> Project
Click New button under Project SDK: Add the latest SDK and Click OK.
After running You will see error is resolved..
In my case the nuance was that I got a invalid source release 11 (instead of OP's 8). I tried all the solutions above
gradle jvm version , java compilter bytecode version, module language.
I set all the above to java 8 (or 1.8) as that is the library I have on my machine.
The issue was that the build.gradle file had
sourceCompatibility = '11'
Changed this to 8 and it stopped throwing 'compile failed invalid release 11' error
solution reference: github forum

Compiling Android project with JDK 1.7

My Android project is set up to use Maven, so I imported into IntelliJ from the pom.xml file. The compiler settings in IntelliJ are correctly pointing to 1.7, but when I try to compile I get:
Error: java: javacTask: source release 1.7 requires target release 1.7
My settings for the maven compiler plugin has both source and target set to this, so that's not the problem. However, in the module settings I see that my module is using
Maven Android API 19 Platform (java version "1.6.0_37")
If I try changing this manually in IntelliJ when I open my project after closing it it gets set back. I think that it has something to do with the android-maven-plugin, but I don't see where to force it to use java 1.7.
There is nothing in the Android Maven Plugin that forces Java 6. It must have to do IntelliJ and its Maven integration somehow. Or some other related configuration.

Categories