157

Despite specifying JDK 1.7 in all project settings (including in File -> Project Structure -> Project :: Project SDK), the following error is produced by IntelliJ 13 when trying to compile some simple Java 7 code which does use the diamond operator:

java: diamond operator is not supported in -source 1.5
(use -source 7 or higher to enable diamond operator)

Is there any other place in the configuration where the expected -source 7 option should be enabled?

PNS
  • 19,295
  • 32
  • 96
  • 143
  • 10
    Tried you: `File -> Project Structure -> Project :: Project language level` change to `Diamonds, ARM, multi-catch etc` ? – Adam Shakhabov Jan 08 '14 at 20:55
  • 43
    Yes. As it turns out, there is yet another option under File -> Project Structure -> Modules :: Sources (next to Paths and Dependencies) and that has a "Language level" option which also needs to be set correctly. Thanks you all guys for the comments and the answers! :-) – PNS Jan 09 '14 at 21:19
  • duplicate https://stackoverflow.com/questions/21747254/getting-compile-error-for-diamond-operator-in-idea-ide – rofrol Jan 23 '15 at 08:13
  • 38
    There are 3 places that need to be updated. (1) File -> Settings (Ctrl+alt+s for the shortcut) then "Build, Execution, Deployment">Compiler>Java Compiler and change the "Target bytecode version" to your desired Java version. (2 and 3) The other place is under File>Project Structure (Ctrl+Alt+Shift+S)> "Project Settings">"Project" change Project SDK to point to the appropriate version of Java and set the Project language level to the correct version. The code assist only changes the language level for you unfortunately making this an annoyance for new projects. – 8bitme Oct 09 '15 at 09:54
  • 3
    What if I have 200 modules in my project do I have to spend all day reclicking them? –  Nov 10 '15 at 19:42
  • That's too annoying, why we have to change 3 places instead one-off config? why not keep it simple? yes I have about 100 modules to set. – July Feb 16 '16 at 08:18
  • Ran into this just now with a new project in IntelliJ 2016. The error now says `Diamond types are not supported at this language level.` IntelliJ had set the module language level as 5 without asking or prompting, instead of the specified JDK 8 for the project and maven settings. – Patrick M Mar 31 '16 at 17:21
  • See that : http://stackoverflow.com/questions/27037657/stop-intellij-idea-to-switch-java-language-level-everytime-the-pom-is-reloaded – Benj Jun 14 '16 at 09:45

15 Answers15

160

Please check your project/module language levels (Project Structure | Project; Project Structure | Modules | module-name | Sources). You might also want to take a look at Settings | Compiler | Java Compiler | Per-module bytecode version.

Set also this:

File -> Project Structure -> Modules :: Sources (next to Paths and Dependencies) and that has a "Language level" option which also needs to be set correctly.

rofrol
  • 14,438
  • 7
  • 79
  • 77
Peter Gromov
  • 17,615
  • 7
  • 49
  • 35
116

If nothing of this helps (my case), you can set it in your pom.xml, like this:

<properties>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
</properties>

As this cool guy mentioned here: https://stackoverflow.com/a/25888116/1643465

Community
  • 1
  • 1
Djordje Ivanovic
  • 4,151
  • 4
  • 27
  • 49
  • 4
    good idea, but don't commit your hard overrides to a shared project unless you want to blow out all your teammates' settings – quaintm Feb 16 '16 at 18:28
  • 8
    This solved the issue for me, thanks! How can it "blow the teammates' settings"? If the project's target is 1.7 then it's 1.7. It's not like each teammate has a different target environment. – isapir Aug 28 '16 at 06:13
  • 1
    Great, mine is maven project. – huuthang Nov 02 '17 at 13:36
  • 1
    For new maven projects this setting can be added in default maven projects. Use **Preferences | Editor | File and Code Templates | Maven | Maven Projects.xml** ` 1.8 1.8 ` [InteliJ Support Forum](https://intellij-support.jetbrains.com/hc/en-us/community/posts/206922595-Default-java-language-setting-for-every-project) – pRmdk Dec 14 '17 at 15:06
  • This did work for me. But only when I restarted IntelliJ, I tried just rebuilding the project alone and that didn't work. – Metin Dagcilar Dec 19 '17 at 18:27
  • For me it doesn't work using Java 11, but changing IntelijIDEA settings for compiler solved the issue. – invzbl3 Jul 26 '19 at 11:46
52

[For IntelliJ IDEA 2016.2]

I would like to expand upon part of Peter Gromov's answer with an up-to-date screenshot. Specifically this particular part:

You might also want to take a look at Settings | Compiler | Java Compiler | Per-module bytecode version.

I believe that (at least in 2016.2): checking out different commits in git resets these to 1.5.

Per-module bytecode version

Community
  • 1
  • 1
Birchlabs
  • 7,437
  • 5
  • 35
  • 54
  • 6
    I gave up on maintaining these at their correct numbers (for some reason they change — I assume when I do `git checkout`). I found that I was able to safely delete entries in this list; the module falls back to using "Project bytecode version". – Birchlabs Jun 21 '16 at 12:54
  • These numbers also revert back to 1.5 for me unexpectedly. Only hard coding the compiler source and target versions in the pom.xml file works consistently. – sheldonkreger May 25 '17 at 17:45
  • Note the "Project bytecode version" setting near the top of this screenshot, as well as the highlighted "Target bytecode version". – Basil Bourque Feb 14 '18 at 00:39
  • @Birchlabs Thank you, I think this is the right answer. – chrips Jun 20 '19 at 01:57
  • 1
    This was the only solution that fixed the problem for me, IntelliJ IDEA 2019.1 – João Rocha da Silva Sep 01 '19 at 23:50
  • This worked for me today in 2019.2, the above POM fix fixed mvn compile etc. but I and running a intelij run configuration which still failed. Upgrading this setting fixed it for me. – SystemsInCode Oct 19 '19 at 08:41
22

Alternatively, you can apply maven-compiler-plugin with appropriate java version by adding this to your pom.xml:

<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>
OlgaMaciaszek
  • 3,662
  • 1
  • 28
  • 32
  • This worked after exiting project deleting .idea folder and .iml file all of which are recreated when opening the project again. – Andrew Aug 22 '19 at 12:47
  • 1
    Pressing the "refresh Maven" button in idea should work. – OlgaMaciaszek Aug 23 '19 at 13:10
  • @OlgaMaciaszek refreshing was required for the solution to work. Thanks! – payne Oct 17 '19 at 15:47
  • This option worked for me directly without removing any file within the project. I am using Intellij2022.2, For me other option which is adding property in maven.compiler.* didn't worked. – Abhishek Verma Dec 15 '22 at 08:29
15

I tried making changes to Intellij IDEA as below:

1.

File >> Settings >> Build, Execution, Deployment >> Compiler >> Java Compiler >> project bytecode version: 1.8 >> Per-module bytecode version: 1.8

2.

File >> Project Structure >> Project Settings >> Project >> SDK : 1.8, Project Language : 8 - Lambdas
File >> Project Structure >> Project Settings >> Modules >> abc : Language level: 8 - Lambdas

but nothing worked, it reverted the versions to java 1.5 as soon as I saved it.

However, adding below lines to root(project level) pom.xml worked me to resolve above issue: (both of the options worked for me)

Option 1:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Option 2:

<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>
Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38
12

File->Project structure->Project Settings->Project->Project Language level

File->Project structure->Project Settings->Modules->Language level

Change level using drop down

ketankk
  • 2,578
  • 1
  • 29
  • 27
4

In your command line(Unix terminal) Go to your project root folder, and do this

find . -type f -name '*.iml' -exec sed -i '' s/JDK_1_5/JDK_1_8/g {} +

This will change the language level property in all your project .iml files from java 1.5 to java 1.8.

  • Not sure yet but you may need to also replace JDK version in `compiler.xml` at the root of the project (IntelliJ 2017.2): `sed 's/target="1.5"/target="1.8"/g' .idea/compiler.xml` – Oleksandr Volynets Oct 06 '17 at 07:56
  • compiler.xml may be specific to your project. This command targets only *.iml files which are only needed by intellij. You may have to adjust language settings in your maven pom.xml or in your case compiler.xml independent of the IDE. – Saideep Sambaraju Oct 13 '17 at 20:11
  • 1
    @SaideepSambaraju compiler.xml is present in any buildable idea project that I saw. From what I see, this file is the model of the Settings->compiler (linux) Preferences->Compiler (MacOS). You can use it to change target version by script as Oleksandr said or by hand without UI. – Sergey Sargsyan Feb 20 '18 at 22:09
  • @SergeySargsyan - I just checked it, it is in the .idea folder which I couldn't find by searching. Thanks for pointing it out. – Saideep Sambaraju Feb 21 '18 at 21:00
3

In IntelliJ Community Edition 2019.02, Changing the following settings worked for me

  1. Update File->Project structure->Project Settings->Project->Project Language level to Java 11 (update to the java version that you wish to use in your project) using drop down.

  2. Update File->Project structure->Project Settings->Modules->Language level

  3. Update File->Settings->Build,Execution,Deployment -> Compiler -> Java Compiler-> Project ByteCode Version to java 11.

  4. Update Target version for all the entries under File->Settings->Build,Execution,Deployment -> Compiler -> Java Compiler-> Per module Byte Code Version.

Khyati Elhance
  • 662
  • 6
  • 11
2

First, you need to change the "project bytecode version" under File > Settings, Compiler > Java Compiler

Second, do a full rebuild.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

I have same problem but with different situation. I can compile without any issue with maven in command line (mvn clean install), but in Intellij I always got "java: diamond operator is not supported in -source 1.5" compile error despite I have set the maven-compiler-plugin with java 1.8 in the pom.xml.

It turned out I have remote repository setting in my maven's settings.xml which the project depends on, but Intellij uses his own maven which doesn't have same setting with my local maven.

So my solution was changing the Intellij's maven setting (Settings -> Build, execution, Deployment -> Maven -> Maven home directory) to use the local maven.

jordom
  • 560
  • 6
  • 10
1

One more thing that could cause this is having incorrect version of the <parent> project.

In my case it was pointing to a non-existing project and for some reason IntelliJ downgraded version in settings to 1.5 and later when I fixed it, it was still interpreting target code version as 5 (despite setting it to 11).

syntagma
  • 23,346
  • 16
  • 78
  • 134
1

I managed to fix this by changing settings for new projects:

  1. File -> New Projects Settings -> Settings for New Projects -> Java Compiler -> Set the version

  2. File -> New Projects Settings -> Structure for New Projects -> Project -> Set Project SDK + set language level

  3. Remove the projects

  4. Import the projects

Danylo Zatorsky
  • 5,856
  • 2
  • 25
  • 49
0

I had the following property working for me in IntelliJ 2017

  <properties>
        <java.version>1.8</java.version>       
  </properties>
H.Rabiee
  • 4,747
  • 3
  • 23
  • 35
0

There seems a few places that effect the source (byte code) version in IntelliJ:

• IntelliJ > Preferences >Build Exce Deploy > Java Compiler
    ◦ Module > Target Bytecode Version
• File > Project Structure 
    ◦ Project > Language Level & SDK
    ◦ Module > Language Level
• Maven Properties
         <properties>
             <maven.compiler.source>17</maven.compiler.source>
             <maven.compiler.target>17</maven.compiler.target>
         </properties>
SydMK
  • 425
  • 4
  • 12
0

nothing from other answers helped, except of installing new Intellij Idea version