IntelliJ: Fatal error compiling: invalid target release: 1.8 - java

I'm running a Java Maven project on IntelliJ, and I make sure that this project will be compiled by Java 1.8.
I set JRE to version 8 in Run/Debug Configuration
and also in Project Structure:
My pom.xml file also includes java version 8 by the following:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
and
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
I also set Java compiler option to version 1.8.
When I run the project, I get an error:
Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.0:compile
(default-compile) on project feed_matcher: Fatal error compiling:
invalid target release: 1.8 -> [Help 1]
Do you know how to resolve this issue?
P.S I followed this link: IDEA: javac: source release 1.7 requires target release 1.7 and I still couldn't resolve the problem.

I assume you want to have it in version 8, despite so many comments telling you to set to 7.
You probably missed the setting for the maven runner, this is under Preferences -> Build, Exection, Deployment > Build Tools > Maven > Runner, see attached image. You need to set that to Java 8 as well.

Look in Build Execution, Deployment > Compiler > Java Compiler
Then look for per module bytecode version

Make sure JAVA_HOME is set to JDK7 or JRE7. This is explained in the
following link
Make sure that you open a new shell an run intellij
If this doesnt work, look at at the idea.bat file which is used to run intellij - make sure it uses jdk7/jre7
Make sure the JDKs are defined properly in your project. Look at this link for more information.
Make sure that the source is defined as 1.7 at maven
The above 4 steps always helped me to troubleshoot such issues.

Related

Eclipse 2018-09 won't compile Java 11 source; thinks it is below 1.7

I'm running Eclipse 2018-09 (4.9.0) on Windows 10. I'm using Open JDK 11 GA. I have a Maven project that was specified as using Java 8 source code.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
That compiles fine using Maven 3.5.3 on the command line. That also compiles fine using Eclipse Eclipse 2018-09 (4.9.0).
I changed the compile Java versions to Java 11:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
That still builds fine on the command line using Maven 3.5.3. But in Eclipse 2018-09, I get errors all over the place:
'<>' operator is not allowed for source level below 1.7
Constructor references are allowed only at source level 1.8 or above
Default methods are allowed only at source level 1.8 or above
You get the idea.
I've already used Alt+F5 to update my Maven project (and subprojects) in Eclipse. I've done a complete clean and rebuild.
Because this compiles fine with Maven on the command line, this has to be an Eclipse problem, doesn't it? Sure, Eclipse doesn't support all new Java 11 features, yet, but this code has no Java 11 specific features. What's wrong?
It sounds like Eclipse is not picking up the versions from the pom.
I just tested your pom configuration and verified that it works, either by providing compiler source and target properties like in the question or the new release property as described in this answer, using the latest Java 11 Support for Eclipse 2018-09 plugin.
It is important that the the JDK 11 is correctly set up in the Eclipse preferences. Add the JDK 11 on the "Installed JRE" preference page and then match it with the JavaSE-11 on the "Execution Environment" preference page. Otherwise updating the Maven project will result in the default JDK being used, which is likely the issue that you are having.
Update: Java 11 is fully integrated in Eclipse since Version 4.10 (released 2018-12-19), so one do not need to install this plugin anymore.
For some reason you need to install an additional Eclipse Plugin "Java 11 Support for Eclipse 2018-09 (4.9)" (even in Eclipse Photon 4.9)
It seams that the plugin is not available in Eclipse Marketplace anymore. I someone find its install url, please add it here:
Works for Eclipse - STS 4.0.1 (based on Eclipse 4.9) , Maven 3.6.0, with this Maven Compiler Plugin Configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
This problem is fixed in Eclipse 2018-12. Upgrading to that version will resolve your issue.
No need to add any plugin.
You only need to upgrade eclipse such that it can handle jdk 11 compiler like mentioned in the attachment.
You don't need to upgrade eclipse in case you have the option to set the jdk 11 compiler.
**Note: Go to the Java Compiler post selecting the project properties and then Click on Configure Workspace settings to mention JDK 11.

Eclipse project: Maven Eclipse Plugin set wrong JRE class path container

I have an Eclipse Mars 2 Java8, Maven 3.5.4 based workspace.
I build the project files with mvn eclipse:clean eclipse:eclipse, and watch the following maven output:
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-**1.7**
The default JRE for the workspace isn't Java 7, it is Java 8, like you can see looking at the following Eclipse config file:
<workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.launching.prefs.
that contains the following data:
org.eclipse.jdt.launching.PREF_VM_XML=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?>
<vmSettings defaultVM\="57,org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType13,1538040823497*">
<vmType id\="org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType">
<vm id\="1431694854640" name\="jdk1.7.0_40" path\="C\:\\Program Files\\Java\\jdk1.7.0_40"/>
<vm id\="1447417000092" name\="jdk1.6.0_45" path\="C\:\\Program Files\\Java\\jdk1.6.0_45"/>
<vm id\="1538040823497" name\="jdk1.8.0_65" path\="C\:\\Program Files\\Java\\jdk1.8.0_65"/>
</vmType>
</vmSettings>
As you may notice by looking at the above configuration, the default VM has the vm id "1538040823497", which is named jdk1.8.0_65, and resides in C\:\\Program Files\\Java\\jdk1.8.0_65.
This VM is correctly registered as Workspace Default in Eclipse Preferences, Java/Installed JREs, is marked as a "perfect match" within the Execution Environment JAVASE-1.8.
I cannot see anything why Maven Eclipse Plugin considers
org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7 as the correct choice, and not JAVASE-1.8.
I already deleted all JREs and registered them newly.
Now there's exactly one 1.6, 1.7 and 1.8 Environment, each with a "perfect match", and 1.8 is checked as default (as you can see above).
I have to manually correct the project each time I generated it (Edit Build Path, change JRE library from 7 to 8), since all files using Java 8 features like streams or lambda functions signal compiler errors unless I assign the correct JRE manually.
I already tried and manipulate org.eclipse.jdt.launching.prefs, redefine all JREs, pray, curse or ask an Ouija board, to no avail, always Java 7 is assigned by the eclipse plugin (version 2.10, by the way).
Any ideas, anyone?
In your pom file, try to use this:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
You may also reference this link:
https://dzone.com/articles/maven-eclipse-and-java-9
Or if all fails, try eclipse oxygen, a newer version (not the newest release but much better than Mars IMO) that has Maven Integration plugin pre-installed, all you need is to add a m2e-connector plugin.

Errors regarding Jdk5 in intellij even though pom.xml explicitly sets source to 7

I have opened a maven project in Intellij (14.1.4 Ultimate) and the JDK is not being recognized. When attempting to run one of the programs the following error occurs - and in any case the file has a bunch of "red"s in it when viewed in the IDE:
Error:(55, 50) java: diamond operator is not supported in -source 1.5
(use -source 7 or higher to enable diamond operator)
This is strange because the normal settings are in place to use java7:
In pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
In the project - JDK level is set to 7:
Likewise in the Module:
But we get all sorts of issues when attempting to compile (even after reimporting maven projects):
Note: this project does build/run on command line using maven.
Also note: I have already tried blowing away the Intellij project and rebuilding from scratch. No change in behavior.
Update The following is a result of following (accepted) answer from #Peter Lawrey. We can see that the jdk got mysteriously set to 1.5. I have updated it to 1.7 manually.
Sometimes IntelliJ gets confused though I don't know why. The setting you need is
File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler
Find your module and change it to version 1.7 instead of 1.5.
I have projects with many modules and the same parent pom which sets this and sometimes just one of the many modules will think it's 1.5 when the rest are 1.8. i.e. the same configuration across many modules.

IntelliJ imports project from POM using wrong JDK version

We have a Maven based Android build, and we just made the switch from JDK 6 to 7.
This came with its share of IntelliJ problems though. What happens is that every time it detects a change in the POM, and reimports/refreshes the project, it returns to selecting the old "Module SDK", the one that's configured to use Java 6:
Even if I manually delete these SDKs from the "Platform Settings" dialog, they keep reappearing as "Maven Android API 19 Platform (N)" where N is the number used to disambiguate it from all the other (identical) SDKs.
I should mention that we do specify in the POM that Java 7 is targeted. I tried to set both the compiler plugin language level, and the maven.compiler.* properties (not sure if that accomplishes the same thing or not), without luck:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
shouldn't IntelliJ pick that up and always configure the project to use a Java 7 SDK? Am I missing something?
I noticed that the problem disappears when I remove any references to 1.6 SDKs entirely in IntelliJ. Not surprising I guess, but also not viable since I have other projects that still rely on the presence of a Java 6 SDK.
I encountered a very similar issue with Maven projects I'd created using IntelliJ (version 14.x in my case). I'd configured IntelliJ to use JDK 8 in the Project Settings but the IDE continued to highlight issues in my code (e.g. complaining about the usage of #Override).
It turns out that the Maven Settings take precedence here, which in my case defaulted to JDK 1.5 (hence the IDE redlines). Changing the settings here does resolve the issue, but only temporarily because they revert back whenever the Maven projects are reimported, or when IntelliJ is restarted.
The permanent fix is to explicitly declared the JDK version in your Maven pom file, as explained in these items.
stop IntelliJ IDEA to switch java language level everytime the pom is reloaded (or change the default project language level) by #vikingsteve
IDEA: javac: source release 1.7 requires target release 1.7 by #bacchus
Here's what they've said you need to add to your pom.xml file.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
These settings get reflected in the Maven Settings in the IDE and resolved the issue for me.
It will pick up the jdk that you choose in project structure please change it there.
File > project structure > project setting > project > project sdk choose 1.7.
If 1.7 is not present go to
File > project structure > Platform setting > SDKs addd 1.7 there.
It's also important to note that you need to change the runner (jdk level) of your maven.
Maven > Runner > JRE

compiling down a version - eclipse, maven

I was recently stung by some code making it through our deployment cycle without throwing any compile errors when it should have (we thought)...
The code in question being using the new static method Integer.compare which is since Java 1.7.
The server environment is run on Java 1.6. While our development environments have Java 1.7 installed.
Our assumption was that setting the project preferences to JavaSE-1.6 compliance would at least give us compile warnings on the code in question, however no warning or error is visible in eclipse.
Project > properties > java compiler > JDK Compliance > Use compliance from execution environment 'JavaSE-1.6' on the java build path
Secondarily to that, we use maven to compile the final deployment. The pom is clearly directed to comply with the 1.6 compiler:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
</configuration>
</plugin>
However the maven build runs successfully with the problem code.
How can I tell both maven and eclipse to fail when code will not work in an earlier Jvm than it is being compiled by?
Thanks, Paul.
Use the maven animal sniffer plugin to tell you when you use APIs that aren't backward compatible. I'm also told that Java 1.7 has a feature for this, but I have no personal experience with it.
Install java 1.6 in the development environment, then right click on the project in eclipse an go to Properties->Java Build Path. Go to the Libraries tab and remove the java 1.7 JRE, then add the java 1.6 JRE.
I'm not familiar enough with maven to answer that half.

Categories

Resources