Intellij idea &引用;无主清单属性“;使用IntelliJ IDEA创建Kotlin jar时

Intellij idea &引用;无主清单属性“;使用IntelliJ IDEA创建Kotlin jar时,intellij-idea,kotlin,jvm,manifest,manifest.mf,Intellij Idea,Kotlin,Jvm,Manifest,Manifest.mf,当从我的Kotlin代码创建一个jar并运行它时,它说“没有主清单属性”。 查看manifest.mf时,它包含以下内容: Manifest-Version: 1.0 Manifest-Version: 1.0 Main-Class: MyMainClass 查看源中的文件时,它包含以下内容: Manifest-Version: 1.0 Manifest-Version: 1.0 Main-Class: MyMainClass 当手动将源清单复制到jar时,它运行得非常好 如果任何依赖J

当从我的Kotlin代码创建一个jar并运行它时,它说“没有主清单属性”。 查看manifest.mf时,它包含以下内容:

Manifest-Version: 1.0
Manifest-Version: 1.0
Main-Class: MyMainClass
查看源中的文件时,它包含以下内容:

Manifest-Version: 1.0
Manifest-Version: 1.0
Main-Class: MyMainClass
当手动将源清单复制到jar时,它运行得非常好


如果任何依赖JAR具有
MANIFEST.MF
文件,它将覆盖定义
主类的自定义JAR

为了解决此问题,您应执行以下操作:

  • 禁用
  • 因此,具有
    META-INF/MANIFEST.MF
    文件的项目是列表中的第一个
  • 您的自定义
    MANIFEST.MF
    将由IntelliJ IDEA和artifact获取
有关更多详细信息,请参阅


您也可以使用Gradle或Maven来生成胖罐子。

我在Gradle和Kotlin中遇到了这个错误。 我必须在我的
build.gradle.kts
中添加一个显式清单属性:

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.example.MainKt"
    }
}
tasks.withType{
显示{
属性[“主类”]=“com.example.MainKt”
}
}

从中,最好创建一个fatJar任务,以便在遇到
java.lang.NoClassDefFoundError
错误时使用所有运行时依赖项。在构建脚本中添加以下任务定义

tasks.jar {
manifest {
    attributes["Main-Class"] = "MainKt"
}
configurations["compileClasspath"].forEach { file: File ->
    from(zipTree(file.absoluteFile))
}
}
  • 然后从右侧边栏再次显示jar任务(tasks | build | jar)

  • 谢谢,成功了!我将您的答案标记为已接受答案。我使用此页面选择了gradle路线: