Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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". 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

like image 856
Luna Avatar asked Nov 27 '25 22:11

Luna


2 Answers

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

like image 158
Sylhare Avatar answered Dec 01 '25 03:12

Sylhare


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

like image 34
Domagoj Tokić Avatar answered Dec 01 '25 03:12

Domagoj Tokić



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!