https://kotlinlang.org logo
#ktor
Title
# ktor
m

Martin Gaens

06/28/2022, 12:45 PM
I'm making a library that uses Ktor Client in the background, but for some reason when I use it (when I use the
.jar
), I get a
NoClassDefFoundError
. This is the full stack trace:
Copy code
Exception in thread "main" java.lang.NoClassDefFoundError: io/ktor/client/engine/cio/CIO
	at com.gitlab.djsushi123.kotlintelegrambotmother.Bot.<init>(bot.kt:22)
	at com.gitlab.djsushi123.kotlintelegrambotmother.BotKt.buildBot(bot.kt:57)
	at MainKt.main(Main.kt:7)
	at MainKt$main$2.invoke(Main.kt)
	at MainKt$main$2.invoke(Main.kt)
	at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$1.invokeSuspend(IntrinsicsJvm.kt:205)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:115)
	at kotlin.coroutines.jvm.internal.RunSuspendKt.runSuspend(RunSuspend.kt:19)
	at MainKt.main(Main.kt)
Caused by: java.lang.ClassNotFoundException: io.ktor.client.engine.cio.CIO
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	... 10 more
What am I doing wrong? The library uses Gradle. This is my `build.gradle.kts`:
Copy code
plugins {
    id("org.jetbrains.kotlin.jvm") version "1.7.0"
    kotlin("plugin.serialization") version "1.7.0"

    `java-library`
}

version = "0.0.1-alpha"

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

    implementation("org.jetbrains.kotlin:kotlin-stdlib")

    api("io.ktor:ktor-client-core:2.0.2")
    api("io.ktor:ktor-client-cio:2.0.2")

    api("io.ktor:ktor-client-content-negotiation:2.0.2")
    api("io.ktor:ktor-serialization-kotlinx-json:2.0.2")

    api("io.ktor:ktor-client-resources:2.0.2")

    // Use the Kotlin test library.
    testImplementation("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration.
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
a

Aleksei Tirman [JB]

06/28/2022, 1:51 PM
Hey. How do you build a JAR file?
m

Martin Gaens

06/28/2022, 2:04 PM
@Aleksei Tirman [JB] I run the
build
gladle task
The JAR file gets output into
lib/build/libs
a

Aleksei Tirman [JB]

06/28/2022, 2:05 PM
As far as I know, this command builds only your artifact. So to run you application you need add all the dependencies to the classpath.
Alternatively, you can use something like ShadowJar to pack your application/library with all dependencies in a single JAR.
m

Martin Gaens

06/28/2022, 2:09 PM
So you have some useful links on how to add the dependencies to the classpath? I couldn't find any resources on how to properly create Kotlin JVM (not multiplatform) libraries through Gradle.
a

Aleksei Tirman [JB]

06/28/2022, 2:11 PM
m

Martin Gaens

07/03/2022, 11:38 AM
Thank you very much!
2 Views