IntelliJ imports project from POM using wrong JDK version - java

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

Related

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.

Eclipse Default JRE Preference Installed JRE Not Working

I went to the Preference of Eclipse and changed the default JRE to the Java SE 8, and also changed the Compiler settings of Eclipse to be 1.8 JDK compliance. I even removed the other JREs.
However, when I import a project, as you can see from the Project Explorer, the project name is "SpringService", somehow the JRE System Library is JavaSE-1.6 and they are still using 1.6 as default!
Can anyone tell me what I did wrong? Otherwise every new project I have to manually go to the properties and change it manually! Hate it!
This post might be relevant to enter link description here SO question.
When you set your Installed JREs preference, you want to select the root directory of a full Java Development Kit installation, not just a bare JRE installation. Eclipse needs access to the supporting libraries that are bundled with a full JDK, but absent from a base JRE install.
Also, if you are importing a project, do you mean that you are importing an existing, pre-configured Eclipse project? Or just the directory structure that contains the source code? If you are importing an existing Eclipse project, it may have already been set up with project-specific settings (it's hard to give advice with being able to poke around in the settings).
Check the plugin section in the pom.xml to see if it contains the following entry.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
This will overwrite the default preference. A lot of maven project archetype uses 1.6. Just change them to 1.8 and do a Maven->Update Project.
U can you 'Build Path' on the project ,then remove the jre1.6, add the user libiary .

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.

How to make Maven use Eclipse workspace default JRE?

I'm using Spring Tool Suite and m2e to convert some of our existing projects to Maven projects. The project in question uses jdk1.6.0_20 which is named [jdk1.6] in Eclipse. When I do Maven -> Update project, though, it replaces that jre with the standard [JavaSE-1.6]. While they seem to point to the same libraries, the change in name causes a bunch of exceptions like:
Access restriction: The type WindowsPopupMenuSeparatorUI is not
accessible due to restriction on required library C:\Program
Files\Java\jdk1.6.0_20\jre\lib\rt.jar
My pom.xml has this:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Is there any way to get Maven/m2e to use the default workspace JRE instead of replacing it with a specific one in the .classpath?
Go into Configure Build Path on the project and go to the Libraries tab.
Remove the JRE System Library
Click on "Add Library..." and select "Workspace Default JRE"
That will give you the current JRE and not specify a specific JRE
Adding maven-compiler-plugin to your pom forces maven to use given version of Java:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
However if you have both jre and jdk installed on your system (for a particular java version), maven may choose jre and then complain that it needs JDK. This may happen after using Maven > Update Project in Eclipse.
To solve this, in Eclipse, right click on JRE System Library > Properties > Environments and select JavaSE-1.8. If you have more than one compatible JREs in your system then tick the jdk1.8.x > OK > select JavaSE-1.8 (jdk1.8.x) as Execution environment > OK
Now maven should choose Java version 1.8 and Eclipse will tell maven to use jdk1.8 as the default JRE for this java version.
It turns out there doesn't seem to be a way to do this with Maven. Instead I changed the error to a warning in Eclipse settings while I work with the owners of the offending code to fix the problems.

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