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

如何修改Heroku应用的Java运行时版本?部署Spring Boot遇版本问题

解决Heroku部署JDK11 Spring Boot应用时自动安装JDK8的问题

问题描述

部署基于JDK11的Spring Boot应用到Heroku时,构建日志显示自动安装了OpenJDK 1.8,即使已在项目根目录创建并提交了包含java.runtime.version=11的system.properties文件。启动时抛出UnsupportedClassVersionError,提示类文件版本55.0(对应JDK11)无法被JDK8(支持到52.0)识别,导致应用崩溃。

构建日志片段:

-----> Uploading build...
       - success
-----> Deploying...
remote: 
remote: -----> Building on the Heroku-22 stack
remote: -----> Using buildpack: heroku/jvm
remote: -----> heroku-deploy app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 106.2M
remote: -----> Launching...
remote:        Released v14

启动异常核心片段:

2022-10-23T18:03:42.408259+00:00 app[web.1]: Exception in thread "main" java.lang.UnsupportedClassVersionError: dsi/senior/SeniorCareApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

解决方案

1. 验证system.properties的有效性

  • 确保文件放在项目根目录,内容严格为:
    java.runtime.version=11
    
  • 执行git status确认文件已加入Git追踪,若未添加,执行git add system.properties && git commit -m "Specify JDK 11 runtime"后重新部署。

2. 清理Heroku构建缓存

旧缓存可能复用之前的JDK版本,执行以下命令清理后重新部署:

heroku builds:cache:purge
git push heroku main

3. 同步项目构建配置的Java版本

确保项目构建文件中编译版本与JDK11一致:

  • Maven(pom.xml):
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    
  • Gradle(build.gradle):
    sourceCompatibility = '11'
    targetCompatibility = '11'
    

提交修改后重新部署。

4. 重置Heroku Buildpack

确认使用官方JVM构建包,执行以下命令重置:

heroku buildpacks:set heroku/jvm
git push heroku main

5. 手动设置Heroku环境变量

若上述方法无效,直接指定JDK版本环境变量:

heroku config:set JAVA_VERSION=11
git push heroku main

内容的提问来源于stack exchange,提问作者Benamor Omar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 03:20:33