kotlin 程序错误:jar 文件中没有主清单属性

diw*_*wuu 3 kotlin

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

fun main(args: Array<String>) {
    println("Hello, World!")
}
Run Code Online (Sandbox Code Playgroud)

然后我用kotlinc编译

$kotlinc hello.kt -include-runtime -d hello.jar
Run Code Online (Sandbox Code Playgroud)

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

$java -jar hello.jar
Run Code Online (Sandbox Code Playgroud)

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

$no main manifest attribute, in hello.jar
Run Code Online (Sandbox Code Playgroud)

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

Syl*_*are 10

我遇到了这个答案,同时遇到了 Kotlin 和 gradle 相同的问题。我想打包以使 jar 工作,但一直出现起球错误。

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

fun main(args: Array<String>) {
    println("Hello, World!")
}
Run Code Online (Sandbox Code Playgroud)

所以这里是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) }
    })
}
Run Code Online (Sandbox Code Playgroud)

因此,一旦您gradle clean build可以执行以下操作:

gradle run
> Hello, World!
Run Code Online (Sandbox Code Playgroud)

假设您的投影仪使用 jarbuild/libs/hello.jar假设settings.gradle.kts您已经设置rootProject.name = "hello"

然后你可以运行:

fun main(args: Array<String>) {
    println("Hello, World!")
}
Run Code Online (Sandbox Code Playgroud)


小智 0

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