IDEA: javac: source release 1.7 requires target release 1.7
Asked Answered
C

18

514

When running a JUnit test, using IntelliJ IDEA, I get

enter image description here

How can I correct this?

  • Using SDK 1.7
  • Module language level is 1.7

Maven build works fine. (That's why I believe this in IDEA configuration issue)

Constantino answered 15/10, 2012 at 16:55 Comment(0)
C
789

Most likely you have incorrect compiler options imported from Maven here:

compiler options

Also check project and module bytecode (target) version settings outlined on the screenshot.

Other places where the source language level is configured:

  • Project Structure | Project

project

  • Project Structure | Modules (check every module) | Sources

sources

Maven default language level is 1.5 (5.0), you will see this version as the Module language level on the screenshot above.

This can be changed using maven-compiler-plugin configuration inside pom.xml:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

or

<project>
  [...]
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  [...]
</project>

IntelliJ IDEA will respect this setting after you Reimport the Maven project in the Maven Projects tool window:

reimport

Creon answered 15/10, 2012 at 17:26 Comment(9)
Correct. Is this setting common to all modules in a project, or is it specific to a given module?Constantino
This setting is per project, affects all modules.Creon
Is there any reason why after fixing this and applying that it would revert back to some incorrect value (either via maven changes or something)?Budget
@Patrick, your pom.xml java compiler plug-in configuration may specify Java version that will override this setting next time you make changes to the pom.xml (if automatic reimport is enabled) or reimport the project manually.Creon
@Creon I changed my pom.xml after changing this once much earlier (was waiting on other incoming changes to the pom), so that sounds like a pretty reasonable explanation. ThanksBudget
What should i put as a command line in the yellow part as you remarked it?Reasoning
@Reasoning leave it blank.Creon
You may need to 'synchronize' pom.xml in IntelliJ after making updates.Shondrashone
The Setting the -source and -target of the Java Compiler page suggests an even shorter addition to the POM file that worked for me, four lines: <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>Neighbor
P
362

IntelliJ 15, 2016 & 2017

Similar to that discussed below for IntelliJ 13 & 14, but with an extra level in the Settings/Preferences panel: Settings > Build, Execution, Deployment > Compiler > Java Compiler.

enter image description here

IntelliJ 13 & 14

In IntelliJ 13 and 14, check the Settings > Compiler > Java Compiler UI to ensure you're not targeting a different bytecode version in your module.

enter image description here

Petrology answered 18/1, 2014 at 21:2 Comment(6)
I had to delete my modules and let it auto resolve (leave blank for jdk default)Malapropos
Works for latest Idea14.1Circuitry
So what's the fix here?Conney
@AlikElzin-kilaka, set the target level to whatever you need.Petrology
Can someone explain why would they put specific module settings in the global preferences config screen, not in the module settings??Hippolytus
@MK I second your question. I have the module's JDK set to 1.8, the Language Level set to 8, and then there's this random f'ing setting over in Settings that still has the module set to 1.7. It's absurd.Cruzcruzado
O
62

In IntelliJ IDEA 14.1 the "Target bytecode version" is in a different place.

The following change worked for me:

File > Settings... > Build, Execution, Deployment > Compiler > Java Compiler : change Target bytecode version from 1.5 to 1.8

enter image description here

enter image description here

Obstetrics answered 7/4, 2015 at 22:12 Comment(2)
or it is better to leave it blank, when it can be determined from jdk!Calmative
Why is there a different configuration than maven's?Conney
K
42

Have you looked at your build configuration it should like that if you use maven 3 and JDK 7

<build>
    <finalName>SpringApp</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        ...
    </plugins>
    ...
</build>
Karly answered 25/8, 2013 at 11:29 Comment(0)
K
23

I ran into this and the fix was to go to Project Settings > Modules > click on the particular module > Dependencies tab. I noticed the Module SDK was still set on 1.6, I changed it to 1.7 and it worked.

Kendrickkendricks answered 13/12, 2012 at 4:20 Comment(1)
I tried this... I ended up having to delete the individual modules and leaving them to auto resolve.Malapropos
S
15

I've found required options ('target bytecode version') in settings > compiler > java compiler in my case (intelij idea 12.1.3)

Subsequence answered 15/6, 2013 at 10:57 Comment(0)
W
10

Modify the compiler setting file of the project in the following path and change the 'target' to 1.7:

/project/.idea/compiler.xml

<bytecodeTargetLevel>
  <module name="project-name" target="1.7" />
</bytecodeTargetLevel>
Wolverhampton answered 8/6, 2015 at 14:14 Comment(1)
Thanks this worked for me! Don't forget to do "Make Module '[your app name]'" by right clicking on the projectLeaved
A
5

I resolved it by setting the field blank:

Settings > Compiler > Java Compiler > Project bytecode version

Than IntelliJ uses the JDK default version.

Altazimuth answered 20/12, 2014 at 12:8 Comment(0)
F
4

From one moment to the other I also got this error without a clear reason. I changed all kinds of settings on the compiler/module etc. But in the end I just recreated the IntelliJ project by reimporting the Maven project and the issue was solved. I think this is a bug.

IntelliJ 12 129.961

Filly answered 20/7, 2013 at 9:31 Comment(1)
Same here for IntelliJ 14.1.2. Reimported the project and it was fixed.Unwished
R
4

I've hit this after just minor upgrade from IntelliJ IDEA 14 to v14.1. For me changing an edit of top/parent pom helped and then clicked re-import Maven (if it is not automatic).

But it maybe just enough to Right Click on module(s)/aggregated/parent module and Maven -> Reimport.

Rann answered 6/4, 2015 at 8:58 Comment(1)
this worked for me! i did it for my top-level module and my child modulesMerylmes
T
2

I resolved bellow method

File >> Project Structure >> Project >> Project Language Level --> do set proper version (ex: 1.5)

Tinstone answered 12/12, 2014 at 6:29 Comment(0)
G
1

check .idea/misc.xml sometimes you need to change languageLevel="JDK_1_X" attribute manually

Goring answered 24/8, 2013 at 0:49 Comment(0)
U
1

If it is a Gradle project, in your build.gradle file, search for following settings:

sourceCompatibility = "xx"
targetCompatibility = "xx"

For all subrpojects, in your root build.gradle file, you may put:

subprojects { project ->
    sourceCompatibility = "1.7"
    targetCompatibility = "1.7"
}

Although you can manually set language levels in Idea > Settings, if it is a Gradle project, Idea automatically synchronizes module .iml files from Gradle settings ( tested with Idea 15+). So all your manual changes are overriden when gradle is refreshed.

Based on Gradle documentation, if these are not set, then current JVM configuration is used.

Usia answered 12/4, 2016 at 1:14 Comment(0)
A
1

I found another way to run into this error. You can get this if you have been re-organizing your directory structure, and one of your poms is pointing to the old parent which no-longer configures javac (because that configuration was moved to a middle level). If this happens the top level defaults to 1.5 and the misbehaving lower level pom inherits it.

So another thing to check when you see this error is that your pom structure is matching your directory structure properly.

Alessi answered 24/6, 2016 at 17:59 Comment(0)
D
1

If Maven build works fine, try to synchronizing structure of Maven and IntelliJ IDEA projects.

In the Maven tool window, click refresh button refresh. On pressing this button, IntelliJ IDEA parses the project structure in the Maven tool window.

Note that this might not help if you're using EAP build, since Maven synchronization feature may be broken sometimes.

Dinka answered 27/7, 2017 at 9:20 Comment(0)
P
1

If all the previous solutions haven't worked for you (which was my case), you can delete intellij config files:

  • project_directory/.idea/compiler.xml
  • project_directory/.idea/encodings.xml
  • project_directory/.idea/misc.xml
  • project_directory/.idea/modules.xml
  • project_directory/.idea/vcs.xml
  • project_directory/.idea/workspace.xml
  • etc.

Intellij will regenerate new ones later. However, BE CAREFUL, this will also delete all intellij configuration made on the projet (i.e: configuration of debug mode, ...)

Pliocene answered 23/4, 2018 at 12:13 Comment(1)
I had a case when this didn't help either. But since I knew that build worked fine in CLI, I knew the issue had to be somewhere in the IDEA. JDK version/level was set to 17 everywhere. Finally simply using "File / Invalidate Caches ..." solved the problem. I just don't know if deleting project's ".idea/" was also required.Mcgurn
T
1

You need to change Java compiler version in in build config.

enter image description here

Thaine answered 8/10, 2018 at 10:15 Comment(0)
C
0

Make sure right depency is selected. File > Project Structure

Select your project and navigate to Dependencies tab. Select right dependancy from dropdown or create new.

Continuation answered 9/11, 2016 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.