"No main manifest attribute" when creating Kotlin jar using IntelliJ IDEA
Asked Answered
A

5

17

When creating a jar from my Kotlin code and running it, it says "No main manifest attribute". When looking at the manifest.mf, it has this content:

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.

Screenshot of my artifact settings

Ambulator answered 26/2, 2019 at 19:13 Comment(0)
C
2

If any of the dependent jars has a MANIFEST.MF file, it will override your custom one which defines the 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.

Continental answered 26/2, 2019 at 21:36 Comment(1)
Thanks, it worked! I marked your answer as accepted answer I took the gradle route using this page: mkyong.com/gradle/gradle-create-a-jar-file-with-dependenciesAmbulator
L
17

I got this error with Gradle and Kotlin. I had to add in my build.gradle.kts an explicit manifest attribute:

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

Lozengy answered 22/4, 2020 at 18:47 Comment(0)
L
4

So far the simplest and best solution I've found is using Ktor Gradle Plugin

plugins {
    id("io.ktor.plugin") version "2.2.3" // Check if it's latest
}

application {
    mainClass.set("com.example.ApplicationKt")
}

ktor {
    fatJar {
        archiveFileName.set("fat.jar")
    }
}

Run it with ./gradlew buildFatJar

Lindsey answered 1/3, 2023 at 9:48 Comment(0)
C
2

If any of the dependent jars has a MANIFEST.MF file, it will override your custom one which defines the 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.

Continental answered 26/2, 2019 at 21:36 Comment(1)
Thanks, it worked! I marked your answer as accepted answer I took the gradle route using this page: mkyong.com/gradle/gradle-create-a-jar-file-with-dependenciesAmbulator
A
2

1.Add the following task definition in the build script

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.
Always answered 20/4, 2021 at 3:27 Comment(0)
S
0

For Spring boot apps:

What worked for me (gradle kotlin) in build.gradle.kts

  1. add spring boots plugin &. apply dependency management
plugins {
    id("org.springframework.boot") version "2.6.7"
}
apply(plugin = "io.spring.dependency-management")
  1. set your main class
springBoot {
    mainClass.set("com.example.Application")
}

Found this all by reading up on spring-boot docs found here

Shoreward answered 3/5, 2022 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.