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

Ktor项目Jar运行报NoClassDefFoundError: io/ktor/server/cio/CIO求解决

Ktor项目Jar包运行时NoClassDefFoundError问题解决

问题场景

本地运行Ktor项目一切正常,执行./gradlew installDist生成Jar包后,运行java -jar ir.coffee-0.0.1.jar时抛出NoClassDefFoundError,找不到io/ktor/server/cio/CIO类。

项目代码

Application.kt

fun main() {
    embeddedServer(
        factory = CIO, port = 8080, host = "0.0.0.0"
    ) {
        configDependencyInjection()
        configSerialization()
        configJwtAuthentication()
        configStatusPages()
        configRequestValidation()

        configUserRouting()

        Database.connect(
            url = "jdbc:postgresql://localhost:5432/coffeehouse",
            driver = "org.postgresql.Driver",
            user = "postgres",
            password = "74279744fz"
        )

        transaction {
            SchemaUtils.drop(Users, CoffeeHouses)
            SchemaUtils.create(Users, CoffeeHouses)
        }

    }.start(wait = true)
}

build.gradle.kts

val ktorVersion: String by project
val kotlinVersion: String by project
val logbackVersion: String by project
val exposedVersion: String by project
val koinVersion: String by project
val postgresVersion: String by project

plugins {
    application
    kotlin("jvm") version "1.7.20"
    id("io.ktor.plugin") version "2.1.2"
    id("org.jetbrains.kotlin.plugin.serialization") version "1.7.20"
}

group = "ir.coffee"
version = "0.0.1"
application {
    mainClass.set("ir.coffee.ApplicationKt")

    val isDevelopment: Boolean = project.ext.has("development")
    applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}

repositories {
    mavenCentral()
}
tasks.withType<Jar> {
    manifest {
        attributes["Main-class"] = application.mainClass
    }
}

dependencies {
    implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-core-jvm:$ktorVersion")
    implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-cio-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-auth:$ktorVersion")
    implementation("io.ktor:ktor-server-auth-jwt:$ktorVersion")
    implementation("io.ktor:ktor-server-request-validation:$ktorVersion")
    implementation("io.ktor:ktor-server-status-pages:$ktorVersion")

    implementation("ch.qos.logback:logback-classic:$logbackVersion")

    implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
    implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
    implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
    implementation("org.postgresql:postgresql:$postgresVersion")


    implementation("io.insert-koin:koin-ktor:$koinVersion")
    implementation("io.insert-koin:koin-logger-slf4j:$koinVersion")

    testImplementation("io.ktor:ktor-server-tests-jvm:$ktorVersion")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")
}

错误信息

Exception in thread "main" java.lang.NoClassDefFoundError: io/ktor/server/cio/CIO
at ir.coffee.ApplicationKt.main(Application.kt:16)
at ir.coffee.ApplicationKt.main(Application.kt)
Caused by: java.lang.ClassNotFoundException: io.ktor.server.cio.CIO at         
   java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 2 more

解决建议

原因分析

./gradlew installDist生成的Jar是瘦Jar,仅包含项目自身代码,未打包依赖库,运行时无法找到Ktor等第三方类。

方案一:生成胖Jar(推荐)

已引入io.ktor.plugin插件,直接执行以下命令生成包含所有依赖的胖Jar:

./gradlew shadowJar

生成的Jar位于build/libs/目录,命名格式为ir.coffee-0.0.1-all.jar,运行该Jar即可:

java -jar build/libs/ir.coffee-0.0.1-all.jar

建议移除build.gradle.kts中手动配置的tasks.withType<Jar>代码块,Ktor插件会自动处理主类和依赖打包逻辑,避免冲突。

方案二:使用installDist生成的启动脚本

./gradlew installDist会在build/install/ir.coffee/bin/目录生成平台对应的启动脚本,直接运行脚本即可自动加载所有依赖:

  • Linux/macOS:
    ./build/install/ir.coffee/bin/ir.coffee
    
  • Windows:
    build\install\ir.coffee\bin\ir.coffee.bat
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 16:40:53