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

Heroku部署Java EE应用报错:需用-source 7及以上启用菱形运算符

Fixing the "diamond operator requires -source 7 or higher" Error on Heroku Deployment

Hey there, I’ve run into this exact issue before when deploying Java apps to Heroku—let’s get this sorted out for you!

The core problem here is that your Maven project is trying to compile Java code using a version older than Java 7, but the diamond operator (<>) was introduced in Java 7. Heroku’s default build environment might be using a lower version, or your project’s Maven configuration isn’t specifying the right source/target versions. Here are the most reliable fixes:

  • Update your Maven compiler plugin configuration in pom.xml
    This is the most direct way to enforce the Java version for compilation. Add or modify the maven-compiler-plugin section in your build plugins:

    <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> <!-- Java 8 is a safe choice; you can use 11+ if your app supports it -->
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    Make sure the source and target values match—using Java 8 or higher will fully support the diamond operator.

  • Set the Java runtime version for Heroku
    Even if your Maven config is correct, Heroku might default to an older Java runtime. Create a system.properties file in your project root directory with this line:

    java.runtime.version=1.8
    

    You can replace 1.8 with a newer version like 11 or 17 if your application is compatible—just ensure it’s at least 1.7.

  • Verify local compilation first
    Before pushing to Heroku, run a local build to catch any remaining issues:

    mvn clean install -e
    

    The -e flag will show the full stack trace, which can help identify if there are other plugins or dependencies overriding your Java version settings.

Once you’ve made these changes, commit the updated files to Git and re-run git push heroku master—that should resolve the diamond operator compilation error.

内容的提问来源于stack exchange,提问作者Julio Amorim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:47:35