Problems building .jar files with Kotlin/IntelliJ/Gradle from: gradlew build and from IDE

Hi, I am learning to code apps using Kotlin.

In order to learn Kotlin, I am trying to build console apps and desktop apps.

So far, I can run my hello world apps using gradlew run (or from the IDE).

However, I cannot seem to be able to build a .jar file which is runnable using:

kotlin App.jar
or
java - jar App.jar

PS: I am new with Kotlin, but I am familiar with most of the build tools from all the languages in the top 10 of the TIOBE index.

  • One thing that puzzles me is why the default app generated by Gradle initor IntelliJ seems to come without a manifest file.
  • Another thing which I am unsure of is the actual qualified name of the main class : Is it “MainKt” or "projectName.Mainkt"or “org.example.MainKt” or something else (the build.gradel.kts file points to “MainKt” it seems).
  • When using the menu command: File->Project structure to add “Artifacts”, the main class is not detected by the dialog box.
  • When I edit a manifest file containing the following:
Manifest-Version: 1.0
Main-Class: MainKt

the class name MainKt is highlighted in red (with the hint: Cannot resolve class ‘MainKt’).

I am using:

  • Java : openjdk version "18.0.1.1" 2022-04-22
  • Kotlin: openjdk version "18.0.1.1" 2022-04-22
  • Gradle:
------------------------------------------------------------
Gradle 7.4.2
------------------------------------------------------------

Build time:   2022-03-31 15:25:29 UTC
Revision:     540473b8118064efcc264694cbcaa4b677f61041

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          18.0.1.1 (Homebrew 18.0.1.1+0)
OS:           Mac OS X 12.4 aarch64

  • IntelliJ:
IntelliJ IDEA 2022.1.2 (Community Edition)
Build #IC-221.5787.30, built on May 31, 2022
Runtime version: 11.0.15+10-b2043.56 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.4
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 8
Non-Bundled Plugins:
    org.jetbrains.kotlin (221-1.7.0-release-281-IJ5591.52)
    org.jetbrains.kotlin-js-inspection-pack-plugin (0.0.9)

Kotlin: 221-1.7.0-release-281-IJ5591.52

Jar is a very simple format that is best suited for distributing individual libraries or small applications. For applications in general, it is better to distribute them as multiple jar files, similarly as e.g. Windows application is usually uncompressed to a directory with multiple separate libraries, resources, etc. If we have to distribute a bigger application as a single jar file, then this is possible and this technique is often called a “fat jar”, but this is really some kind of a hack and usually requires a little more tinkering with the build configuration.

In order to build the application package with Gradle, use application plugin:

plugins {
    ...
    application
}

application {
    mainClass.set("MainKt")
}

Then run ./gradlew distZip (or distTar).

Manifest is fully optional. I don’t even remember when I had to create one for the last time.

In Java/Kotlin we always identify classes by their package names and class names. At runtime JVM has no concept of the project name.

2 Likes

Thank you, I’ll have a look at the application plugin!