How do I create and run jar files in kotlin
# gradle
i
How do I create and run jar files in kotlin
c
same way you would do it in Java. Kotlin compiles to
*.class
files just like java does.
c
i
I am new to this. Does it work with gradle ?
This is my set up.
Copy code
plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.30'
    id 'application'
}

group 'boringworks.kaas'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin/org.boringworks'
}

jar {
    manifest {
        attributes 'Main-Class': 'org.boringworks.Main'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

archivesBaseName = "kaas-function"

mainClassName = "com.boringworks.Main"
message has been deleted
c
Your source layout should be:
Copy code
├ build.gradle
├ src
  ├ main
    ├ kotlin
      ├ org
        ├ boringworks
          ├ Main.kt
then you do not need sourceSets manipulation, just remove that. Also you don't need the
from
part of the
jar
configuration And lastly, since it is Kotlin and you're probably using package level
main
function in the
Main.kt
file, your
'Main-Class'
value should be
org.boringworks.MainKt
and not
Main
full build.gradle:
Copy code
plugins {
   id 'org.jetbrains.kotlin.jvm' version '1.3.30'
   id 'application'
}

group 'boringworks.kaas'
version '1.0-SNAPSHOT'

repositories {
   jcenter()
}

dependencies {
   implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

compileKotlin {
   kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
   kotlinOptions.jvmTarget = "1.8"
}

jar {
   manifest {
       attributes 'Main-Class': 'org.boringworks.MainKt'
   }
}

archivesBaseName = "kaas-function"

application {
    mainClassName = "org.boringworks.MainKt"
}
i
!!! Thank you man
🙂 1
@Czar Do you have an Idea why I can’t use classes from my dependencies: I always get this
Copy code
$  java -jar build/libs/kaas-function-1.0-SNAPSHOT.jar                
Exception in thread "main" java.lang.NoClassDefFoundError: arrow/core/Option
        at org.boringworks.MainKt.main(Main.kt:20)
        at org.boringworks.MainKt.main(Main.kt)
Caused by: java.lang.ClassNotFoundException: arrow.core.Option
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 2 more
I added this to my dependencies:
Copy code
compile("io.arrow-kt:arrow-docs:$arrow_version")
    compile("io.arrow-kt:arrow-syntax:$arrow_version")
    compile("io.arrow-kt:arrow-typeclasses:$arrow_version")
    compile("io.arrow-kt:arrow-core-data:$arrow_version")
    compile("io.arrow-kt:arrow-extras-data:$arrow_version")
    compile("io.arrow-kt:arrow-core-extensions:$arrow_version")
    compile("io.arrow-kt:arrow-extras-extensions:$arrow_version")
    kapt("io.arrow-kt:arrow-meta:$arrow_version")
c
You are not specifying class path. Your jar only contains your own classes.
i
ah
Is there a tutorial for all this I only found things for android but not for plain kotlin
c
Yes, but I don't have any links handy. It is basic JVM stuff though. So Java SE official docs + gradle docs. It's slower than doing tutorial, but IMHO worth it.
i
Ok. Can you send me the text I need to add
😂 Sorry Man.. I just need this to work
for now at least
c
My consulting services cost money 🙂 read the docs, this is basic stuff and not kotlin related at all
s
in case you want to use the Kotlin DSL with gradle (which means that the config file is now called build.gradle.kts, it will also produce a fat jar - a jar with all dependencies - that's what the shadow plugin does)
Copy code
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
   kotlin("jvm") version "1.3.30"
   application
   id("com.github.johnrengelman.shadow") version "5.0.0"
}

application {
   mainClassName = "uk.co.mypath.MyMainClass"
}

group = "uk.co.mypath"
version = "1.0-SNAPSHOT"

repositories {
   mavenCentral()
   jcenter()
}

dependencies {
   implementation(kotlin("stdlib-jdk8"))
}

tasks {
   withType<KotlinCompile> {
       kotlinOptions.jvmTarget = "1.8"
   }
}
👌🏽 1