【问题标题】:kotlin program error: no main manifest attribute in jar filekotlin 程序错误:jar 文件中没有主要清单属性
【发布时间】:2019-07-07 11:40:48
【问题描述】:

我写了一个简单的 kotlin helloworld 程序 你好.kt

fun main(args: Array<String>) {
    println("Hello, World!")
}

然后我用 kotlinc 编译了它

$kotlinc hello.kt -include-runtime -d hello.jar

没有错误并且生成了hello.jar。 当我运行它时

$java -jar hello.jar

它说 hello.jar 中没有主要清单属性

$no main manifest attribute, in hello.jar

我无法弄清楚这个问题。 我的kotlin版本是1.3.40,JDK版本是1.8.0

【问题讨论】:

    标签: kotlin


    【解决方案1】:

    我在遇到与 Kotlin 和 gradle 相同的问题时遇到了这个答案。我想打包让罐子工作,但一直在起球错误。

    使用像 com.example.helloworld.kt 这样的文件包含您的代码:

    fun main(args: Array<String>) {
        println("Hello, World!")
    }
    

    这就是文件build.gradle.kts 的样子,让你开始使用 gradle。

    import org.gradle.kotlin.dsl.*
    import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
    
    plugins {
      application
      kotlin("jvm") version "1.3.50"
    }
    
    // Notice the "Kt" in the end, meaning the main is not in the class
    application.mainClassName = "com.example.MainKt"
    
    dependencies {
      compile(kotlin("stdlib-jdk8"))
    }
    
    tasks.withType<KotlinCompile> {
      kotlinOptions.jvmTarget = "1.8"
    }
    
    tasks.withType<Jar> {
        // Otherwise you'll get a "No main manifest attribute" error
        manifest {
            attributes["Main-Class"] = "com.example.MainKt"
        }
    
        // To add all of the dependencies otherwise a "NoClassDefFoundError" error
        from(sourceSets.main.get().output)
    
        dependsOn(configurations.runtimeClasspath)
        from({
            configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
        })
    }
    

    所以一旦你gradle clean build 你可以这样做:

    gradle run
    > Hello, World!
    

    假设您的投影仪使用build/libs/hello.jar 中的jar 假设您在settings.gradle.kts 中设置了rootProject.name = "hello"

    然后就可以运行了:

    java -jar hello.jar
    > Hello, World!
    

    【讨论】:

      【解决方案2】:

      尝试升级到 1.3.41 版本并使用 JDK 1.11+。

      【讨论】:

        猜你喜欢
        • 2018-02-10
        • 2020-07-28
        • 1970-01-01
        • 2021-05-29
        • 2021-07-30
        • 2015-10-01
        • 2022-12-23
        • 2020-12-10
        相关资源
        最近更新 更多