Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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.

like image 949
Matthias Avatar asked Sep 04 '14 13:09

Matthias


2 Answers

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.

like image 160
Donnacha Avatar answered Nov 08 '22 21:11

Donnacha


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.

like image 26
StackFlowed Avatar answered Nov 08 '22 20:11

StackFlowed