You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Maven项目中钻石运算符不支持的-source 1.5错误?

Fixing "diamond operator is not supported in -source 1.5" Error in Maven + IntelliJ IDEA

Hey there, let's get this sorted for you! That error pops up because your project is defaulting to Java 1.5 (which doesn't support the diamond operator <> introduced in Java 7) for compilation—both in Maven and IntelliJ IDEA. Here's how to fix it step by step:

1. Update Your Maven Configuration (pom.xml)

Maven's compiler plugin is likely using the old Java 1.5 default. Add or update the maven-compiler-plugin in your pom.xml to specify a Java version that supports the diamond operator (Java 7 or higher; I recommend 8+ since it's widely used):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version> <!-- Use a recent, stable version -->
            <configuration>
                <source>1.8</source> <!-- Replace with your target Java version (e.g., 11, 17) -->
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

This tells Maven explicitly which Java version to use for compiling your code, so running mvn install will no longer throw the source 1.5 error.

2. Sync IntelliJ IDEA with Maven Settings

IntelliJ might still be using the old Java settings even after updating pom.xml. Let's fix that:

  • Open Project Structure (use the shortcut Ctrl+Alt+Shift+S on Windows/Linux, Cmd+; on Mac):
    • Go to the Project tab: Set "Project SDK" to your target Java version (matches what you put in pom.xml) and "Project language level" to the corresponding level (e.g., 8 for Java 1.8).
    • Go to the Modules tab: Select each module, switch to the Sources tab, and set "Language level" to match the project level.
  • Open IDE Settings (Ctrl+Alt+S or Cmd+,):
    • Search for "Java Compiler" in the settings search bar.
    • Set "Project bytecode version" to your target Java version. Ensure none of the modules have an overridden version unless you need it (uncheck "Override compiler parameters per-module" if you want Maven to manage this).

3. Reload Maven Project

After updating pom.xml and IDE settings, force IntelliJ to re-read the Maven configuration:

  • Click the Refresh icon in the right-side Maven panel (looks like a circular arrow).
  • Alternatively, right-click your project in the Project view → Maven → Reload Project.

4. Test the Fix

Now run mvn clean install in your terminal (or via IntelliJ's Maven panel) and try running your project in the IDE. The diamond operator error should be gone!

内容的提问来源于stack exchange,提问作者Kirill Barsukov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:25:39