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

Compose Multiplatform Desktop:run正常,runDistributable执行失败求助

解决Kotlin Multiplatform桌面应用runDistributable时的NoClassDefFoundError问题

问题原因

报错中的com.sun.net.httpserver.HttpHandler属于JDK内置的jdk.httpserver模块,Java 9及以上版本采用模块化系统,该模块不会默认自动加载。本地执行./gradlew run时,JVM环境默认包含了该模块所以运行正常;但构建可分发包时,Compose Desktop的打包流程没有显式声明依赖这个模块,导致启动时找不到类。

解决方法

在build.gradle.kts的compose.desktop配置块中,给可分发包添加JVM启动参数,显式加载jdk.httpserver模块:

修改后的compose.desktop配置代码:

compose.desktop {
    application {
        mainClass = "MainKt"
        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "myApp"
            packageVersion = "1.0.0"
            // 新增:显式引入jdk.httpserver模块
            jvmArgs += "--add-modules jdk.httpserver"
        }
    }
}

验证

修改后重新执行./gradlew runDistributable,生成的可分发包应该能正常启动,不会再出现NoClassDefFoundError。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 21:13:27