June 28, 2023

Kotlin IDEA - create a .jar file

The goal is to generate "bell.jar" that I can run via "java -jar bell.jar" or something reasonably succinct like that.

I am using the Intellij IDEA community edition IDE.
Here is a decent 5 minute video from 2020 that explains how this all can be done:

Use: File -- Project Structure, then click on the "Artifacts" tab on the left.
In my case I get an empty window with a "+" at the upper left. This is exactly right.
Click on "+" (to add an "artifact"). Select .JAR and "include modules/dependencies" via the submenu.

It gives a dialog that wants you to specify a Main Class. This is the class wherein it should find your main method. In my case I just have main() at the top level. The game for that is to specify "projectKt" as the main class if your code is in project.kt. In my case I will use MainKt since my code is in Main.kt. Then click the "OK" button for the dialog. Another window shows the generated artifact and you click "OK" to dismiss that as well.

Now at the top of the GUI (the tool bar?) select Build, then Build Artifacts from the pulldown menu. It shows me "bell.jar" and that is what I want.

Now you have to find bell.jar on your filesystem. In my case it is at:

cd /u1/home/tom/IdeaProjects/bell/out/artifacts/bell_jar
java -jar bell.jar
And it works!. I see:
java -jar bell.jar
Hello socket
Socket[addr=/192.168.0.41,port=1013,localport=54106]
Client sending [bell]
Client receiving [OK]
And it rings the bell in my workshop.

This means I know how to generate projects that anyone with java installed (the JRE anyway) can run.

As for why the IDEA ide calls a .jar file an artifact -- it is far beyond me to say.


Have any comments? Questions? Drop me a line!

Adventures in Computing / tom@mmto.org