[SOLVED] How to turn a mixed Kotlin/Java project (with a Kotlin file as main class) into a JAR?

Answered

Hi there,

 

This may seem like a obvious question, yet after an hour of trying to fix it and a few hours before that last week I still can't figure it out. It seems like nowhere there are actually tutorials trying to create a jar file with a Kotlin-file as main class. I believe this must be possible.
I was especially surprised since most java-related builds were relatively self-explanatory on Idea, this is the first time I get issues while building.

I can't share the full project so I'm going to be as specific as I can.

I have a mixed Java/Kotlin project, with a Gradle build set up, as well as some dependencies in Maven.

When I try to make a JAR out of it I do the following:

  • In 'Project Structure', click Add > Jar > From Modules with Dependencies, then click the 3 dots next to main class and select your kotlin file (that has a main function)
  • Click ok and let idea make your manifest file.
  • Then do Build > Build Artifacts and select your Jar artifact.

For me, now 2 things happen:

1. The META-INF is overwritten by one of the dependencie's version (Jackson) 

Here is the first few rows of the manifest file now residing in META-INF/

Manifest-Version: 1.0
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-SymbolicName: com.fasterxml.jackson.jaxrs.jackson-jaxrs-base
Built-By: tatu
Bnd-LastModified: 1454433161692
Implementation-Vendor-Id: com.fasterxml.jackson.jaxrs
Specification-Title: Jackson-JAXRS-base
Bundle-DocURL: http://wiki.fasterxml.com/JacksonHome/jackson-jaxrs-bas
e

2. When I force my own manifest into the jar, I get this error:

➜ jar git:(master) ✗ java -jar 'test.jar'
Error: Could not find or load main class com.test.main.MainKt

I've been trying to fix this a long time, thanks in advance!

5 comments
Comment actions Permalink

Can't reproduce in a basic example. Please kindly provide any reproducible test case.

0
Comment actions Permalink

Hi Serge, here is an example: https://kitchen.codebuffet.co/index.php/s/AHy3JIkgaKgaJ7v Import the project, then use the artifact to build a jar (a prebuilt jar is also in the out folder) If the artifact isn't there (not sure if artifacts persist within the project folder or saved somewhere else), follow the instruction to add an artifact from the original post. Then you see the jar being successfully build, but the MANIFEST.MF is different from the one in the project (it's been overwritten by one from a dependency)

0
Comment actions Permalink

It's a common issue with jar artifact overriding MANIFEST.MF, see https://youtrack.jetbrains.com/issue/IDEA-116209#comment=27-656589 for the workaround.

Explicitly adding META-INF directory to the artifact and then adding MANIFEST.MF file into this directory on the first position fixes the problem:

0
Comment actions Permalink

Thanks, that works! Side note for others: Be sure to untick the sort alphabetically button next to up and down arrow.

0
Comment actions Permalink

Dear Serge

Thanks from my side, too. I've spent a lot of time in researching a solution for this issue. The time-consuming workaround was to manually tweak the jar-file. The workaround mentioned above is far easier and faster.

Still love jetbrains great tools and their support.

Regards, Daniel

0

Please sign in to leave a comment.