# Build jar with IDEA throws error but fine with Gradle

**URL:** <https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794>\
**Category:** Support\
**Created:** [July 19, 2017, 7:39am UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794 "2017-07-19T07:39:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshuaavalon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/joshuaavalon/32/2934_2.png) [@joshuaavalon](https://discuss.kotlinlang.org/u/joshuaavalon)\
**Post date:** [July 19, 2017, 7:39am UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/1 "2017-07-19T07:39:58Z")

</div>

The app is simple.

**Main.kt**

```kotlin
package app

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

```

**build.gradle**

```nohighlight
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`

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

```

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

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![forinil2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/forinil2/32/2465_2.png) [@forinil2](https://discuss.kotlinlang.org/u/forinil2)\
**Post date:** [July 19, 2017, 11:29am UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/2 "2017-07-19T11:29:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 19, 2017, 12:08pm UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/3 "2017-07-19T12:08:28Z")

</div>

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](https://docs.gradle.org/3.3/userguide/application_plugin.html). 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.

---

<div class="post-metadata">

**Author:** ![joshuaavalon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/joshuaavalon/32/2934_2.png) [@joshuaavalon](https://discuss.kotlinlang.org/u/joshuaavalon)\
**Post date:** [July 20, 2017, 1:57am UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/4 "2017-07-20T01:57:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 20, 2017, 5:57am UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/5 "2017-07-20T05:57:53Z")

</div>

Can you show the build file you used?

---

<div class="post-metadata">

**Author:** ![jajacomp](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@jajacomp](https://discuss.kotlinlang.org/u/jajacomp)\
**Post date:** [August 3, 2017, 10:02pm UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/6 "2017-08-03T22:02:34Z")

</div>

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”  
}

---

<div class="post-metadata">

**Author:** ![joshuaavalon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/joshuaavalon/32/2934_2.png) [@joshuaavalon](https://discuss.kotlinlang.org/u/joshuaavalon)\
**Post date:** [August 4, 2017, 3:50am UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/7 "2017-08-04T03:50:26Z")

</div>

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](https://joshuaavalon.io//2017/07/28/create-executable-artifact-from-kotlin-project.html).

---

<div class="post-metadata">

**Author:** ![CosmicSignal](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@CosmicSignal](https://discuss.kotlinlang.org/u/CosmicSignal)\
**Post date:** [March 21, 2020, 7:35am UTC](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/8 "2020-03-21T07:35:24Z")

</div>

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
