Build jar with IDEA throws error but fine with Gradle

The app is simple.

Main.kt

package app

fun main(args: Array<String>) {
    System.out.print("Hello World!")
}

build.gradle

group 'test'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1.3-2'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

I tried to build a jar file.

  1. I go to File → Project Structure → Project Settings → Artifacts → + → JAR → from modules with dependencies.
  2. I set the Main Class to app.MainKt and Directory for META-INF/MANIFEST.MF to C:\Users\user\IdeaProjects\test\src\main\java.
  3. I go to Build → Artifacts… → Build.

When I try to run it, it shows no main manifest attribute, in test.jar. However, this is the MANIFEST.MF in src\main\java\META-INF

Manifest-Version: 1.0
Main-Class: app.MainKt

After searching around on the internet, add this to build.gradle

jar {
    manifest {
        attributes 'Main-Class': 'app.MainKt'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

Using gradle jar task to build, the jar is fine.

I would like to ask is there something I setup incorrectly or missing something that make IDEA cannot build the jar?

First of all the manifest should be in folder src/main/resources/META-INF, not src/main/java/META-INF.
Once you fix that, you should then check if Intellij sees the manifest using Artifacts window. If it does not there should be Use Existing Manifest… button at the bottom of the Output Layout tab You can use that to add a manifest file from anywhere in your computer.

I am not sure, why are you messing with manifest manually, it is not meant to be that way. Your gradle build does not have any entry point in it, so no wonder, that it can’t find it. The easiest way to define an entry point is via application plugin. There you will want to add main class attribute:mainClassName = app.MainKt.

In your case gradle completes build, but You won’t be able to run it without additional commands. IDEA will probably also complete the build if you ask it to build, but it will definitely fail if you ask it to run an application.

forinil2

src/main/resources/META-INF
Use Existing Manifest…

I have tried both and they do not work for me. I found it only works in src/main/META-INF

darksnake

application plugin

I have tried that by it does not work.

Can you show the build file you used?

I too have this problem.
Idea version is 2017.2.1
I do next:

  1. Create new project Gradle->Kotlin
  2. Create main file in folder kotlin
  3. Create new default artifacts
  4. Build artifacts
  5. Run app:

java -jar Test.jar
no main manifest attribute, in Test.jar

My generated Mainfest is:

Manifest-Version: 1.0
Main-Class: MainKt

Manifest from jar file is:

Manifest-Version: 1.0
Implementation-Title: kotlin-stdlib-jre7
Kotlin-Runtime-Component: Main
Kotlin-Version: 1.1
Implementation-Version: 1.1.3-2
Build-Jdk: 1.8.0_131
Implementation-Vendor: JetBrains

build.gradle file is:
group ‘org.test’
version ‘1.0-SNAPSHOT’

buildscript {
ext.kotlin_version = ‘1.1.3-2’

repositories {
    mavenCentral()
}
dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

}

apply plugin: ‘kotlin’

repositories {
mavenCentral()
}

dependencies {
compile “org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version”
}

compileKotlin {
kotlinOptions.jvmTarget = “1.8”
}
compileTestKotlin {
kotlinOptions.jvmTarget = “1.8”
}

Build artifacts in intellij does not seems to use gradle. You have to use jar task yourself. You may take a look at my note.

I had the same problem, I solved it in this way:
When setting up the artifact, change:
Meta-inf: (…)\src\main\ (you must remove “java”)

Also, there was a problem with resources, solved this way:
When setting up the artifact:
Output Layout > Add copy of > Directory content > resources.

That’s all