Jar file only works with Java classes but not with Kotlin

Actually, I’ve been to fast again and edited my previous post! :joy:
At first: Thank you for your great help!

But I still don’t know, what your solution does… Could you explain that to me please?

Sure. Let’s suppose the JAR you want to create is called ``minimalkotlin.jar```.

When you’re running that JAR through java -jar minimalkotlin.jar the following happens:

  1. The command java looks into the JAR file into META-INF/MANIFEST.MF because it needs to find out which class it is supposed to run (as you didn’t specify a class to run explicitely). There’s a property Main-Class in that file that tells it what the main entry point for that JAR is.
  2. Then, it tries to locate the main class as specified in the JAR
  3. That class references quite a few other classes (because your Kotlin application is referencing classes from the Kotlin runtime internally). That Kotlin runtime basically does all the internal stuff (parameter checks, mapping function names, …)
  4. As a consequence, java has to find those Kotlin runtime classes as well. Unfortunately, it can’t do that if you put a kotlin-runtime.jar into your JAR file. It simply doesn’t know how to handle JAR libraries within another JAR file. (I simplified a little bit here but that should do the trick for now)

In order for java to find those Kotlin runtime classes, the contents of kotlin-reflect.jar and kotlin-runtime.jar need to be present in your JAR. Basically, IDEA is unzipping those two files and dumping their content into what will become your JAR (first two entries in my screenshot). Then IDEA adds the compiler output of your own code (third entry in screenshot).

That KotlinJavaRuntime (Project Library) is automatically generated when you create a new Kotlin project. Basically, it is a container that holds all the JAR files that belong to the Kotlin runtime. You should be able to see it in File > Project Structure and then select Libraries on the left hand side of the dialog. You’ll see that it contains two JARs with binaries and one that holds source code (which will be used when you navigate to the source of a Kotlin runtime class).

If that KotlinJavaRuntime (Project Library) entry is not there (and as a consequence, you are not able to run or compile any Kotlin code from within IDEA itself!), you need to fix that before setting up JAR creation. I believe Tools > Kotlin > Configure Kotlin in Project should do the trick. After that, the Kotlin runtime library object should be available as described above.

Just follow the steps in my other post to set up how IDEA creates your JAR file. Let me know if that helps!

1 Like

Thanks a lot for this detailed explanation, it helped me a lot! :slightly_smiling:

I think it would be quite helpful for beginners to have a step-by-step tutorial on how to set the IDEA up and create .jar files as you described above or at least make IDEA do that by itself or give you a little message that doing what you told me is/could be required.

But what do I have to do, if I want to use gradle or ant? Where do I then have to put these extracted files?

There always is online help: You will be redirected shortly
Unfortunately, it only describes how things are done without given any background on why things are done that way (and why that might be the proper way of doing it).

Frankly, I would focus my attention on core language concepts. Learn the basic IDE stuff that you minimally need to know through trial and error. Don’t be shy to ask! In the beginning, I would absolutely focus my efforts on learning the language of your choice: syntax and classes that are used in basic building blocks, idioms how things are done, data structures and algorithms. Those are things you want to be good at! That knowledge can’t be acquired by reading a book, though. You need to do learning exercises to really be able to do it by yourself. After all, it is not understanding you want but application.

All the surroundings (tooling, packaging, which library to use for what) only have a very short time frame where they are current and useful to you. There’s a new Gradle version every 6-8 weeks, a new IDEA version probably 2-4 times a year, a new hype on twitter at least twice a day. That’s just too much to keep up. Especially in the beginning. Focus on those things when there is a problem at your hand that needs solving. In the beginning only solve those problems if your current set of tools doesn’t do the job anymore.

It isn’t always easy. Actually, it can be quite hard and frustrating at some times. However, there are also those great moments, when you realize how incredibly rewarding creating your programs with your own imagination can be! Remember those, forget the rest.

Good luck for your journey!

I already found this page in my ~5 hours of search but it did not really tell me to extract the runtime… :smile:
Gradle is some really “crazy” thing - I already found out while programming android apps.
Sometimes when I have enough time, I have a look at Kotlin and try to learn a bit more about it. Most of the time I used it to develop some simple android apps because I have a little bit of experience in that with java.

That’s the point. After testing/debugging inside intellij I wanted to have some kind of a “standalone” program and getting to that point already helped me a lot and I could learn quite a lot of things.

Anyway, thanks a lot for helping me! (I don’t think this is the last time this forum will hear from me :stuck_out_tongue_winking_eye:)

One little question is still here:

How can I open the jar file by double clicking it? From the commandline using java -jar xy.jar works fine. But when I just double click it, it instantly closes.
I already changed the Java.exe to open it with. Any suggestions?

Your code doesn’t really output much. When you double click on your jar, it probably opens a window, outputs a line and closes the window again. Blazingly fast.

You could wait for enter before terminating your program:
https://github.com/dittert/minimalkotlin/commit/7a9a80faaa61f403515984b156c5eff8fddfb33f

Here’s some more background on this: java - Make the console wait for a user input to close - Stack Overflow

I know about this issue from java/c++, so I already put a readline in the program and everything works as intended, as long as I use java -jar in cmd…

fun main(args: Array<String>) { val input = readLine() print(input) }
When using cmd it waits for my input and after that prints it out. But it does not wait when I double click it.
From what I found out by now, this is not a “code-sided” issue but has to do with how/what is executed on that double click (I already used a different java.exe in the “open with”-dialog.

Most probably -jar is not used as an argument when opening the .jar file.

For those who are reading today, don’t worry, you aren’t missing kotlin-runtime.jar, it has been replaced with kotlin-stdlib.jar.

As a reference, this article talks about the same issue if you are using Java gradle plugin with jar: Article

jar {
    manifest {
        attributes 'Main-Class': 'com.preslavrachev.imdbparser.MainKt'
 }

 // This line of code recursively collects and copies all of a project's files
 // and adds them to the JAR itself. One can extend this task, to skip certain
 // files or particular types at will
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
1 Like

Yes! Thank you, this was what I was struggling to find to create an executable .jar

If anyone still struggles to create a jar in intellij maven project that uses kotlin class as main, here’s where I found a solution: https://kotlinlang.org/docs/reference/using-maven.html#self-contained-jar-file