963

Using IntelliJ IDE can't compile any projects. Screenshots of settings below:

Used JDK:

http://gyazo.com/b6e32119af7b04090d890cad04db6373

Project SDK and Language level:

http://gyazo.com/55a5fc9f7f2bb721a04780ce9d74eeab

Language Level:

http://gyazo.com/143bffad63fd89cafc231298729df2fc

Anybody have any ideas?

ayusha
  • 474
  • 4
  • 12
Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • 1
    That doesn't look like an error from "pure Intellij"; aren't you using an ant buildscript or something? – fge Apr 27 '15 at 06:33
  • 6
    possible duplicate of [IDEA: javac: source release 1.7 requires target release 1.7](http://stackoverflow.com/questions/12900373/idea-javac-source-release-1-7-requires-target-release-1-7) – BuZZ-dEE Aug 26 '15 at 18:41
  • https://stackoverflow.com/a/42993827/2685581 – Ajay Kumar Aug 22 '17 at 09:21

25 Answers25

2048
  1. Go to File > Settings > Build, Execution, Deployment > Compiler > Java Compiler If on a Mac, it's under Intellij IDEA > Preferences... > Build, Execution, Deployment > Java Compiler
  2. Change Target bytecode version to 1.8 of the module that you are working for.

If you are using Maven

Add the compiler plugin to pom.xml under the top-level project node:

<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>

(Hoisted from the comments.)

Note: If you don't mind reimporting your project, then the only thing you really need to do is change the pom and reimport the project, then IntelliJ will pick up the correct settings and you don't have to manually change them.

Yellowjacket
  • 548
  • 2
  • 7
  • 19
Weslor
  • 22,180
  • 2
  • 20
  • 31
  • Works in IntelliJ 14 Ultimate. – DtechNet Aug 16 '15 at 17:46
  • On older IDEA versions, try: Settings -> Compiler -> Java Compiler -> Per-module bytecode version – Jonik Sep 23 '15 at 08:52
  • 91
    If you're on a Mac, it's under Intellij IDEA > Preferences... > Build, Execution, Deployment > Java Compiler – J.P. Armstrong Sep 26 '15 at 14:08
  • In IntelliJ v14 it's File --> Other Settings --> Default Settings --> Build, Execution, Deployment --> Java Compiler. – Peter Feb 10 '16 at 15:22
  • 5
    For Intellij newer than 2016.2, there's one more place to check: `Project Structure -> Project Settings -> Modules -> Denpendencies -> Module SDK`. – Bruce Sun Aug 26 '16 at 06:41
  • if you are building with gradle, add sourceCompatibility = 1.8 to your project build.gradle file – vanomart Sep 11 '16 at 13:21
  • 2
    When you have a POM hierarchy with more than two levels and your compiler plugin is defined in the parent POM, IntelliJ might not recognize it at the deepest level. I say might not, because for some other modules it does, even when they extend the same parent POM. – Dormouse Sep 25 '16 at 19:05
  • For IntelliJ 2016.3 it is a little different path. Change it at: Build, Execution, Deployment > Compiler > Java Compiler – PHPGuru Feb 16 '17 at 20:17
  • Add 3.6.1 after becose Maven >=3.0 will have warning. – Fortran Jul 12 '17 at 13:41
  • Why the IDE has 1.5 as default: "Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with." https://maven.apache.org/plugins/maven-compiler-plugin/ – flow2k May 21 '18 at 20:05
  • Tested on Android Studio 4.1.1 is inside: `File -> Project Structure -> Modules` – Ido Nov 19 '20 at 23:55
215

You need to go to Settings and set under the Java compiler the following: enter image description here

also check the Project Settings

Gregory Mazur
  • 2,429
  • 4
  • 16
  • 26
118

This looks like the kind of error that Maven generates when you don't have the compiler plugin configured correctly. Here's an example of a Java 8 compiler config.

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!-- ... -->

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

<!-- ... -->

</project>
Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • 10
    For other Maven newbies like myself, this should be under `project`, `build`, and then `plugins`. – DJH Aug 07 '15 at 09:38
  • 5
    Also if `1.8` and `1.8` is configured, the above error can occur. – BuZZ-dEE Aug 26 '15 at 18:40
  • 1
    This worked for me, thank you! The version tag did not resolve (I changed it to match my version of Maven) so I commented it out. I believe Maven should automatically fetch the correct latest version in that case, based on this thread: https://stackoverflow.com/questions/17239394/maven-plugin-version-not-specified. – Boris Jan 03 '16 at 23:23
91

The quickest way I found:

  • press:CTRL + SHIFT + A (For Mac + SHIFT + A)
  • type: java compiler
  • press: ENTER

In the Settings window, set the Target bytecode to 1.8

(or 9 for java9)

Anonsage
  • 8,030
  • 5
  • 48
  • 51
snovelli
  • 5,804
  • 2
  • 37
  • 50
32

There are two ways to solve this problem:

  1. Set settings (File -> Settings -> Build, Execution, Deployment -> Java Compiler): enter image description here
  2. Add a build section to your pom.xml: enter image description here
Lord Nighton
  • 1,670
  • 21
  • 15
19

Many answers regarding Maven are right but you don't have to configure the plugin directly.

Like described on the wiki page of the Apache Maven Compiler Plugin you can just set the 2 properties used by the plugin.

<project>
  [...]
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  [...]
</project>
Marvin Richter
  • 591
  • 6
  • 11
  • 1
    @ Marvin Richter I tried your solution but it does not work with IntelliJ - My guess is that it's correct in pure maven build (command-line), but IntelliJ just ignore these properties. I had to configure the plugin as mentioned in other solutions. – Benoit Dec 23 '16 at 15:36
  • 1
    No I did not, because the other solutions worked. And now I migrated my project to gradle. – Benoit Jan 09 '17 at 08:22
  • 6
    Works like a charm after you do Right click on pom.xml -> Maven -> Reimport. This should be the accepted answer for Maven users. – ptkvsk Jan 13 '17 at 10:23
  • You actually need the plugin `org.apache.maven.plugins:maven-compiler-plugin` in your pom or gradle file, what in the documentation intented to say is that having that property set in the pom properties will be enough, basically because they set the defaults values of the plugin, e.g. ${maven.compiler.source} – EliuX Oct 19 '17 at 07:29
  • This should be the accepted answer. Worked for me on IntelliJ 2018.2 on mac os 10.13.5 – user674669 Jul 09 '18 at 20:54
  • This was the only thing that fixed it for me after hours of searching. All other settings were right (and worked fine for years) and then suddenly one day not having this changed everything and my maven couldn't compile Java 8 anymore. – Tim Jul 10 '18 at 17:28
  • It's the correct solution; just that IntelliJ has a bug (still!) ignoring this may cause some inconvenience... – foo Jan 25 '20 at 18:37
14

I fixed this by going to Project Structure -> Modules, find the module in question, click on Dependencies tab, change Module SDK to Project SDK.

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
9

I fixed it just by changing target compile version to 1.8. Its in:

File >> Settings >> Build, Execution, Deployment >> Compiler >> Java Compiler

Vishal
  • 1,963
  • 2
  • 20
  • 23
  • 1
    Thanks, Vishal. We have to specify project bytecode version, or at least per-module bytecode version with Intellij Idea. Just setting it in pom.xml for maven-compiler-plugin didn't solve the problem for me. – WebComer Jun 13 '16 at 13:27
7

You need to go to the /.idea/compiler.xml and change target to required jdk level.

enter image description here

Muhammad Usman
  • 863
  • 1
  • 11
  • 18
5

In my case I fixed this issue by opening .iml file of project (it is located in project root folder and have name same as the name of project) and changing line <orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" /> to <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />

I had everything configured as in others answers here but by some reason Idea updated .iml file incorrectly.

Cloud
  • 983
  • 11
  • 11
  • 2
    It seems sometimes Intellij does not update the config files even when you configured all menus. *Invalidate caches and restart* (after editing the config files), did the trick for me. Strange. – Hendrik Jander Feb 07 '16 at 15:26
  • 1
    Thanks so much. Finally after trying and failing every other solution specified above, this worked. – Nitin Bansal Sep 19 '16 at 18:21
4

I fixed it by modify my POM file. Notice the last comment under the highest voted answer.

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
</plugin>

The source must matches the target.

4

I just re-import maven button, then the error disappeared.

enter image description here

Andreas
  • 5,393
  • 9
  • 44
  • 53
Pham Hung
  • 43
  • 4
4

In your Gradle app level file >> compileOptions add this two lines

android {
  compileOptions {
    ...
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    ...
   }
}
Badr
  • 2,021
  • 1
  • 21
  • 27
3

If you are working with Android-studio 1.3, Follow the below steps -

Go to File - Project Structure

Under modules- app-Properties tab, choose Source Compatibility -1.8 and

Target Compatibility - 1.8.

And you are good to go.

B.shruti
  • 1,589
  • 1
  • 21
  • 41
1

Under compiler.xml file you will find :

<bytecodeTargetLevel>
  <module name="your_project_name_main" target="1.8" />
  <module name="your_project_name_test" target="1.8" />
</bytecodeTargetLevel>

and you can change the target value from your old to the new for me i needed to change it from 1.5 to 1.8

1

With Intellij, using Maven, you must check that Intellij has auto-imported your project. You can check by clicking on the Maven tab on the right of your Editor.

enter image description here

If your Project is not here, then add the pom.xml file by clicking on +.

Obviously, the project must also have the relevant <build/> :

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74
1

I've just spent a while struggling with the same problem. The only thing that worked for me was not using the built mvn (3.3.9) but pointing it to an external downloaded version (3.5.0). Finally the project opened and everything was good.

thunder
  • 501
  • 4
  • 7
1

Don't forget to set dependencies for your module: enter image description here

Hieu Vo
  • 3,105
  • 30
  • 31
1

This issue occurs if your module is configured with Annotation processor and other module is not.Set the same configuration for all the modules as it wold be cyclic dependency.

Gani
  • 422
  • 1
  • 8
  • 16
1

the below code working fine by my side. I just add it in the pom.xml file.

<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>
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
0

For me, the problem was about Maven not able to find proper configurations, since these items were specified in parent pom.

Changing File -> Settings -> Build, Excecution, Deployment -> Maven -> User Settings file to point to my custom settings with proper repositories fixed the problem that was otherwise hiding.

Found out about the problem through Help -> Show log in explorer -> clicking the log file, when previously only got the error in the title and "java.lang.NullPointerException" in the console.

eis
  • 51,991
  • 13
  • 150
  • 199
0

If none of the other answers work, check your Module SDK.

I had this error pop up for me after I had updated the project SDK to 1.8, the Javac compiler to 1.8, etc. The setting that was the problem for me was the "Module SDK".

(on Linux) Go to File > Project Structure... then in the window that opens, select Modules under Project Settings. Select the module in question from the list and then the Dependencies tab and make sure that Module SDK is set appropriately.

dzimney
  • 565
  • 1
  • 5
  • 15
0

I have checked all of the above but the error still occurs.

But reimport all maven Projects (reload button inside Maven Projects panel) works in my case.

snap
  • 1,598
  • 1
  • 14
  • 21
0

The only thing that helped me was to delete .idea/compiler.xml file.

c4da
  • 73
  • 9
-1

Solution of the problem is very simple.You have to open .idea/compiler.xml file on your İdea Project and

You should write appropriate target version

Axmedbek
  • 1
  • 1
  • 1