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

Heroku运行Kotlin Spring Web应用遇NoClassDefFoundError错误求助

Hey there, let's break down why you're seeing that NoClassDefFoundError for kotlin/jvm/internal/Intrinsics when running your Kotlin Spring app on Heroku. That error pops up because your JAR file isn't including the Kotlin standard library (and probably other dependencies) when you run it—here's how to fix it:

1. Make sure you're building a "fat/uber JAR" with all dependencies

Regular JAR files only contain your own code, not the Kotlin standard library, Spring framework, or any other third-party dependencies your app needs. For Kotlin Spring Boot apps, the easiest way to build a self-contained JAR is using Spring Boot's built-in packaging tools:

If you're using Gradle (build.gradle.kts):

Ensure your config enables the bootJar task (which packages all dependencies) and disables the plain jar task:

plugins {
    id("org.springframework.boot") version "your-spring-boot-version"
    id("io.spring.dependency-management") version "your-dependency-management-version"
    kotlin("jvm") version "your-kotlin-version"
    kotlin("plugin.spring") version "your-kotlin-version"
}

// Disable plain JAR, use bootJar instead
tasks.named<Jar>("jar") {
    enabled = false
}

tasks.named<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
    // Point to your main class
    mainClass.set("pl.makzyt.pss_kotlin.MainKt")
}

Run ./gradlew bootJar locally first—this will generate a JAR in build/libs that includes every dependency your app needs. Test it locally with java -jar build/libs/pss_kotlin-1.0-SNAPSHOT.jar to confirm it works before deploying to Heroku.

If you're using Maven (pom.xml):

Add the Spring Boot Maven plugin to repackage your JAR into a self-contained executable:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>pl.makzyt.pss_kotlin.MainKt</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Run mvn package to generate the fat JAR, then test it locally the same way.

2. Configure Heroku to use the right startup command

Instead of manually running java -cp via heroku run bash, create a Procfile in your project root—this tells Heroku how to start your app properly:

web: java -jar build/libs/pss_kotlin-1.0-SNAPSHOT.jar

Heroku will automatically run this command when deploying, ensuring it uses the fat JAR with all dependencies included.

3. Verify Heroku's build process

Check your Heroku build logs (run heroku logs --tail in your terminal) to confirm it's running bootJar (Gradle) or mvn package (Maven) and generating the correct fat JAR. If you see it building a plain JAR instead, double-check your build config.

Once you fix these steps, your app should start without that missing class error!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:02:37