[英]"No main manifest attribute" when creating Kotlin jar using IntelliJ IDEA
When creating a jar from my Kotlin code and running it, it says "No main manifest attribute".从我的 Kotlin 代码创建 jar 并运行它时,它显示“没有主要清单属性”。 When looking at the manifest.mf, it has this content:
查看 manifest.mf 时,它包含以下内容:
Manifest-Version: 1.0
When looking at the file in the source, it has this content:查看源文件中的文件时,它包含以下内容:
Manifest-Version: 1.0
Main-Class: MyMainClass
When manually copying the source manifest to the jar, it runs perfectly.手动复制源清单到jar时,运行完美。
I got this error with Gradle and Kotlin.我在使用 Gradle 和 Kotlin 时遇到了这个错误。 I had to add in my
build.gradle.kts
an explicit manifest attribute:我必须在我的
build.gradle.kts
添加一个显式清单属性:
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "com.example.MainKt"
}
}
From the gradle documentation , it's better to create a fatJar task to englobe all of the runtime dependencies in case you encounter java.lang.NoClassDefFoundError
errors从gradle 文档中,如果遇到
java.lang.NoClassDefFoundError
错误,最好创建一个 fatJar 任务来封装所有运行时依赖项
If any of the dependent jars has a MANIFEST.MF
file, it will override your custom one which defines the Main-Class
.如果任何依赖的 jars 有一个
MANIFEST.MF
文件,它会覆盖你定义Main-Class
自定义文件。
In order to address this problem you should do the following:为了解决这个问题,您应该执行以下操作:
META-INF/MANIFEST.MF
file is the first in the listMETA-INF/MANIFEST.MF
文件的项目在列表中排在第一位MANIFEST.MF
will be picked up by IntelliJ IDEA and displayed for the jar artifact.MANIFEST.MF
将由 IntelliJ IDEA 选取并显示为 jar工件。 See the related issue for more details.有关更多详细信息,请参阅相关问题。
You can also use Gradle or Maven to generate the fat jar instead.您也可以使用 Gradle 或 Maven 来生成胖 jar。
1.Add the following task definition in the build script 1.在构建脚本中添加如下任务定义
tasks.jar {
manifest {
attributes["Main-Class"] = "MainKt"
}
configurations["compileClasspath"].forEach { file: File ->
from(zipTree(file.absoluteFile))
}
}
For Spring boot apps:对于 Spring 引导应用程序:
What worked for me (gradle kotlin) in build.gradle.kts build.gradle.kts 中对我有用的 (gradle kotlin)
plugins {
id("org.springframework.boot") version "2.6.7"
}
apply(plugin = "io.spring.dependency-management")
springBoot {
mainClass.set("com.example.Application")
}
Found this all by reading up on spring-boot docs found here通过阅读此处找到的 spring-boot 文档找到了这一切
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.