简体   繁体   English

使用 IntelliJ IDEA 创建 Kotlin jar 时出现“No main manifest attribute”

[英]"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时,运行完美。

Screenshot of my artifact settings我的神器设置截图

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 errorsgradle 文档中,如果遇到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:为了解决这个问题,您应该执行以下操作:

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))
}
}
  1. Then the jar tasks (Tasks | build | jar) again from the right hand sidebar.然后再次从右侧边栏中执行 jar 任务(Tasks | build | jar)。

For Spring boot apps:对于 Spring 引导应用程序:

What worked for me (gradle kotlin) in build.gradle.kts build.gradle.kts 中对我有用的 (gradle kotlin)

  1. add spring boots plugin &.添加 spring 靴子插件 &。 apply dependency management应用依赖管理
plugins {
    id("org.springframework.boot") version "2.6.7"
}
apply(plugin = "io.spring.dependency-management")
  1. set your main class设置你的主 class
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM