A runnable jar with Kotlin and Gradle

Forketyfork
4 min readNov 15, 2022

In this article, I’ll describe how to build a runnable jar from a Kotlin application using Gradle, and try to clarify some points of confusion and errors you may face in the process. I’ll describe both Groovy and Kotlin ways of configuring the build scripts.

Initial setup

It’s pretty easy to generate a sample Kotlin project with a runnable application. Just fire up your console, create a directory (let’s name it hello), go inside, and run gradle init:

mkdir hello
cd hello
gradle init

This is the summary of configuration choices for an application written in Kotlin and built using a Kotlin script (just select a different option on the “Select build script DSL” stage if you want to use Groovy as your build script language):

Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 4

Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1

Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Kotlin) [1..2] 2

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] yes
Project name (default…

--

--

Forketyfork

Software developer @ JetBrains Space. I write technical how-to articles and occasional rants on software development in general. Opinions are my own.