Ktor java.lang.NoClassDefFoundError: io/ktor/server/cio/CIO - kotlin

I developed a simple project using ktor
here is my Application.kt
fun main() {
embeddedServer(
factory = CIO, port = 8080, host = "0.0.0.0"
) {
configDependencyInjection()
configSerialization()
configJwtAuthentication()
configStatusPages()
configRequestValidation()
configUserRouting()
Database.connect(
url = "jdbc:postgresql://localhost:5432/coffeehouse",
driver = "org.postgresql.Driver",
user = "postgres",
password = "74279744fz"
)
transaction {
SchemaUtils.drop(Users, CoffeeHouses)
SchemaUtils.create(Users, CoffeeHouses)
}
}.start(wait = true)
}
and here is my build.gradle.kts
val ktorVersion: String by project
val kotlinVersion: String by project
val logbackVersion: String by project
val exposedVersion: String by project
val koinVersion: String by project
val postgresVersion: String by project
plugins {
application
kotlin("jvm") version "1.7.20"
id("io.ktor.plugin") version "2.1.2"
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.20"
}
group = "ir.coffee"
version = "0.0.1"
application {
mainClass.set("ir.coffee.ApplicationKt")
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
repositories {
mavenCentral()
}
tasks.withType<Jar> {
manifest {
attributes["Main-class"] = application.mainClass
}
}
dependencies {
implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktorVersion")
implementation("io.ktor:ktor-server-core-jvm:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktorVersion")
implementation("io.ktor:ktor-server-cio-jvm:$ktorVersion")
implementation("io.ktor:ktor-server-auth:$ktorVersion")
implementation("io.ktor:ktor-server-auth-jwt:$ktorVersion")
implementation("io.ktor:ktor-server-request-validation:$ktorVersion")
implementation("io.ktor:ktor-server-status-pages:$ktorVersion")
implementation("ch.qos.logback:logback-classic:$logbackVersion")
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("org.postgresql:postgresql:$postgresVersion")
implementation("io.insert-koin:koin-ktor:$koinVersion")
implementation("io.insert-koin:koin-logger-slf4j:$koinVersion")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktorVersion")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")
}
when I run the project, it works fine and i can make a jar file with
./gradlew installDist command.
but when I try to run the jar file with java -jar ir.coffee-0.0.1.jar command
i get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: io/ktor/server/cio/CIO
at ir.coffee.ApplicationKt.main(Application.kt:16)
at ir.coffee.ApplicationKt.main(Application.kt)
Caused by: java.lang.ClassNotFoundException: io.ktor.server.cio.CIO at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 2 more
any suggestion how can I fix this?

The installDist task packs the project's class files in a JAR without dependencies. So to solve your problem you can either specify paths to the dependencies via the -classpath flag while executing java or pack all dependencies in a JAR (Gradle Shadow plugin).

Related

How can I find the main class of my app when selecting it to create a JAR ? Kotlin

I want to make a JAR to test and deploy my app but it doesn't work, locally it can't find the main attribute in the manifest file, and in the deployement it starts but i have this error :
java.lang.noclassdeffounderror: kotlinx/coroutines/slf4j/mdccontex
W
hen I do it locally using the artifact Jar I can't find the MainClass of my app, it recommands me all sorts of other modules.
I tried modying my gradle, the manifest, the configuration but really nothing works it's depressing, it looks so easy on some youtube videos but it just doesn't work
what I do to make the JAR
The classes i have access to when i try to select them
Here is my Gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.5.10"
application
kotlin("plugin.serialization") version "1.6.10"
}
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven(url = "https://jitpack.io")
}
val ktor_version: String by project
dependencies {
// Fix HTML issue on some responses
implementation("org.apache.commons:commons-text:1.10.0")
// Ktor dependencies
implementation("io.ktor:ktor-client-auth:$ktor_version")
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-client-resources:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-client-logging:$ktor_version")
// Logging dependencies
implementation("ch.qos.logback:logback-classic:1.4.0")
implementation(kotlin("stdlib-jdk8"))
// Database
implementation ("mysql:mysql-connector-java:8.0.30")
implementation ("org.ktorm:ktorm-core:3.5.0")
implementation ("org.ktorm:ktorm-support-mysql:3.5.0")
}
application {
mainClass.set("MainKt")
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
tasks {
jar {
manifest {
attributes["Main-Class"] = application.mainClass
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
configurations.compileClasspath.get().forEach {
from(if (it.isDirectory) it else zipTree(it))
}
}
compileKotlin{
kotlinOptions.jvmTarget = "1.8"
}
}
Thanks in advance
My project had a lot of issues and inconsistency so I started a new one and copied all the files / packages, spent an hour redoing all the imports and plugins that my code actually needed to run and it finally worked, the same comments don't work on the previous project

Publishing SNAPSHOT versions of Android libraries using Android Gradle Plugin 7+

I have an Android library (gradle multi module project) where I publish to two different Nexus Maven2 repositories with strict versioning, a release repository and a snapshot repository.
This has worked well using Android Gradle Plugin 4.0.1.
command:
gradlew -PmavenRepoUrl=https://myserver.com/nexus/repository/project-snapshots -PmavenRepoCredentialType=PasswordType -PmavenUser=$NEXUS_USER -PmavenPassword=$NEXUS_PASSWORD publishDebugPublicationToMavenRepository
build.gradle.kts:
plugins {
id("com.android.library")
id("maven-publish")
...
}
val mavenRepoUrl: String by project
val mavenRepoCredentialType: String by project
val mavenPublicAccessKey: String by project
val mavenPublicSecretKey: String by project
val mavenUser: String by project
val mavenPassword: String by project
afterEvaluate {
publishing {
repositories {
maven(url = mavenRepoUrl) {
when (mavenRepoCredentialType) {
"AwsCredentialType" -> {
credentials(AwsCredentials::class) {
accessKey = mavenPublicAccessKey
secretKey = mavenPublicSecretKey
}
}
"PasswordType" -> {
credentials(PasswordCredentials::class) {
username = mavenUser
password = mavenPassword
}
}
else -> {
logger.log(ERROR, "Unsupported credential type: $mavenRepoCredentialType")
}
}
}
}
publications {
create<MavenPublication>("Release") {
from(components.getByName("release"))
groupId = project.group as String
artifactId = "my-android-project-${project.name}"
version = project.version as String
}
create<MavenPublication>("Debug") {
from(components.getByName("debug"))
groupId = project.group as String
artifactId = "my-android-project-${project.name}"
version = project.version as String
}
}
}
}
But after updating to Android Gradle Plugin 7.0+ I'm getting this error:
Execution failed for task ':core:publishDebugPublicationToMavenRepository'.
> Failed to publish publication 'Debug' to repository 'maven'
> Could not PUT 'https://myserver.com/nexus/repository/project-snapshots/com/myproject/1.1.0-M25/my-project-1.1.0-M25.aar'. Received status code 400 from server: Repository version policy: SNAPSHOT does not allow version: 1.1.0-M25
(When upgrading Android Gradle Plugin I had to remove the "isDebuggable" attribute from the build variants. Maybe that's a clue?).
To troubleshoot I tried explicitly setting the version to "-SNAPSHOT" for debug builds, build.gradle.kts:
publications {
create<MavenPublication>("Debug") {
...
version = (project.version as String) + "-SNAPSHOT"
...
}
}
but then I get this error:
* What went wrong:
Could not determine the dependencies of task ':my-module-2:publishDebugPublicationToMavenRepository'.
> Publishing is not able to resolve a dependency on a project with multiple publications that have different coordinates.
Found the following publications in project ':my-modulue':
- Maven publication 'Release' with coordinates com.myproject:my-module:1.1.0-M25
- Maven publication 'Debug' with coordinates com.myproject:my-module:1.1.0-M25-SNAPSHOT
(related: https://github.com/gradle/gradle/issues/12324)
My setup:
Multi module project with Android library modules
Android Gradle Plugin 7.0.1
Gradle 7.0.2, 7.1.1, 7.2 (doesn't work with any of them)
Kotlin Gradle DSL
Is this a regression caused by the Android Gradle Plugin or is there a way to solve my problem?

kotlin program error: no main manifest attribute in jar file

I wrote a simple kotlin helloworld program
hello.kt
fun main(args: Array<String>) {
println("Hello, World!")
}
Then I compiled it with kotlinc
$kotlinc hello.kt -include-runtime -d hello.jar
there was no errors and hello.jar was generated.
when I ran it
$java -jar hello.jar
it said there is no main manifest attribute in hello.jar
$no main manifest attribute, in hello.jar
I couldn't figure out this problem.
My kotlin version is 1.3.40, JDK version is 1.8.0
I came accross this answer while having the same issue with Kotlin and gradle. I wanted to package to get the jar to work but kept on pilling errors.
With a file like com.example.helloworld.kt containing your code:
fun main(args: Array<String>) {
println("Hello, World!")
}
So here is what the file build.gradle.kts would look like to get you started with gradle.
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
application
kotlin("jvm") version "1.3.50"
}
// Notice the "Kt" in the end, meaning the main is not in the class
application.mainClassName = "com.example.MainKt"
dependencies {
compile(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<Jar> {
// Otherwise you'll get a "No main manifest attribute" error
manifest {
attributes["Main-Class"] = "com.example.MainKt"
}
// To avoid the duplicate handling strategy error
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
// To add all of the dependencies otherwise a "NoClassDefFoundError" error
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
So once you gradle clean build you can either do:
gradle run
> Hello, World!
Assuming your projector using the jar in build/libs/hello.jar assuming that in your settings.gradle.kts you have set rootProject.name = "hello"
Then you can run:
java -jar hello.jar
> Hello, World!
Try to upgrade to version 1.3.41 and using JDK 1.11+.

java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener

I am trying to create a test class (JUnit 5) using Intellij Idea but I get the bellow error. When I created the test class it did NOT show the fix button so I am pretty sure the library is in class path.
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:45)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.TestExecutionListener
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
My build.gradle looks as below:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
group 'ict221'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.11
mainClassName = 'boardgame.gui.RunGame'
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
javafx {
modules = [ 'javafx.controls', 'javafx.fxml' ]
version = "11.0.2"
}
Any help is much appreciate it.
As mentionned by #CrazyCode in comments, with Intellij you need to specify the version of JUnit (because Intellij is bundled with old version of JUnit)
Add this :
dependencies {
...
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.6.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.1")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.6.1")
}
try entering these depencies for junit5 in a gradle project
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.2")
Below is an example of a build.gradle for a simple application with JDK 17, Gradle 7.5.1 and JUnit Jupiter 5.9.0, which causes IntelliJ IDEA to correctly run unit tests with JUnit.
Notice that there is no need to add test dependencies as it is done automatically by configuring testing section.
build.gradle
plugins {
id 'application'
}
group = 'your.app'
version = '0.0.1'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
}
testing {
suites {
test {
useJUnitJupiter('5.9.0')
}
}
}
application {
mainClass = 'your.app.App'
}
src/main/test/your/app/AppTest.java
package your.app;
import org.junit.jupiter.api.Test;
class AppTest {
#Test
void testFeature() {
}
}

How to create a fat JAR with Gradle Kotlin script?

As titled, I'd like to know how to modify the gradle.build.kts in order to have a task to create a unique jar with all the dependencies (kotlin lib included) inside.
I found this sample in Groovy:
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.mkyong.DateUtils'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
But I have no idea how I could write that in kotlin, other than:
task("fatJar") {
}
Here is a version that does not use a plugin, more like the Groovy version.
import org.gradle.jvm.tasks.Jar
val fatJar = task("fatJar", type = Jar::class) {
baseName = "${project.name}-fat"
manifest {
attributes["Implementation-Title"] = "Gradle Jar File Example"
attributes["Implementation-Version"] = version
attributes["Main-Class"] = "com.mkyong.DateUtils"
}
from(configurations.runtime.map({ if (it.isDirectory) it else zipTree(it) }))
with(tasks["jar"] as CopySpec)
}
tasks {
"build" {
dependsOn(fatJar)
}
}
Also explained here
Some commenters pointed out that this does not work anymore with newer Gradle versions.
Update tested with Gradle 5.4.1:
import org.gradle.jvm.tasks.Jar
val fatJar = task("fatJar", type = Jar::class) {
baseName = "${project.name}-fat"
manifest {
attributes["Implementation-Title"] = "Gradle Jar File Example"
attributes["Implementation-Version"] = version
attributes["Main-Class"] = "com.mkyong.DateUtils"
}
from(configurations.runtimeClasspath.get().map({ if (it.isDirectory) it else zipTree(it) }))
with(tasks.jar.get() as CopySpec)
}
tasks {
"build" {
dependsOn(fatJar)
}
}
Note the difference in configurations.runtimeClasspath.get() and with(tasks.jar.get() as CopySpec).
Here are 4 ways to do this. Note that the first 3 methods modify the existing Jar task of Gradle.
Method 1: Placing library files beside the result JAR
This method does not need application or any other plugins.
tasks.jar {
manifest.attributes["Main-Class"] = "com.example.MyMainClass"
manifest.attributes["Class-Path"] = configurations
.runtimeClasspath
.get()
.joinToString(separator = " ") { file ->
"libs/${file.name}"
}
}
Note that Java requires us to use relative URLs for the Class-Path attribute. So, we cannot use the absolute path of Gradle dependencies (which is also prone to being changed and not available on other systems). If you want to use absolute paths, maybe this workaround will work.
Create the JAR with the following command:
./gradlew jar
The result JAR will be created in build/libs/ directory by default.
After creating your JAR, copy your library JARs in libs/ sub-directory of where you put your result JAR. Make sure your library JAR files do not contain space in their file name (their file name should match the one specified by ${file.name} variable above in the task).
Method 2: Embedding the libraries in the result JAR file (fat or uber JAR)
This method too does not need any Gradle plugin.
tasks.jar {
manifest.attributes["Main-Class"] = "com.example.MyMainClass"
val dependencies = configurations
.runtimeClasspath
.get()
.map(::zipTree) // OR .map { zipTree(it) }
from(dependencies)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
Creating the JAR is exactly the same as the previous method.
Method 3: Using the Shadow plugin (to create a fat or uber JAR)
plugins {
id("com.github.johnrengelman.shadow") version "6.0.0"
}
// Shadow task depends on Jar task, so these configs are reflected for Shadow as well
tasks.jar {
manifest.attributes["Main-Class"] = "org.example.MainKt"
}
Create the JAR with this command:
./gradlew shadowJar
See Shadow documentations for more information about configuring the plugin.
Method 4: Creating a new task (instead of modifying the Jar task)
tasks.create("MyFatJar", Jar::class) {
group = "my tasks" // OR, for example, "build"
description = "Creates a self-contained fat JAR of the application that can be run."
manifest.attributes["Main-Class"] = "com.example.MyMainClass"
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
val dependencies = configurations
.runtimeClasspath
.get()
.map(::zipTree)
from(dependencies)
with(tasks.jar.get())
}
Running the created JAR
java -jar my-artifact.jar
The above solutions were tested with:
Java 17
Gradle 7.1 (which uses Kotlin 1.4.31 for .kts build scripts)
See the official Gradle documentation for creating uber (fat) JARs.
For more information about manifests, see Oracle Java Documentation: Working with Manifest files.
For difference between tasks.create() and tasks.register() see this post.
Note that your resource files will be included in the JAR file automatically (assuming they were placed in /src/main/resources/ directory or any custom directory set as resources root in the build file). To access a resource file in your application, use this code (note the / at the start of names):
Kotlin
val vegetables = MyClass::class.java.getResource("/vegetables.txt").readText()
// Alternative ways:
// val vegetables = object{}.javaClass.getResource("/vegetables.txt").readText()
// val vegetables = MyClass::class.java.getResourceAsStream("/vegetables.txt").reader().readText()
// val vegetables = object{}.javaClass.getResourceAsStream("/vegetables.txt").reader().readText()
Java
var stream = MyClass.class.getResource("/vegetables.txt").openStream();
// OR var stream = MyClass.class.getResourceAsStream("/vegetables.txt");
var reader = new BufferedReader(new InputStreamReader(stream));
var vegetables = reader.lines().collect(Collectors.joining("\n"));
Here is how to do it as of Gradle 6.5.1, Kotlin/Kotlin-Multiplatform 1.3.72, utilizing a build.gradle.kts file and without using an extra plugin which does seem unnecessary and problematic with multiplatform;
Note: in reality, few plugins work well with the multiplatform plugin from what I can tell, which is why I suspect its design philosophy is so verbose itself. It's actually fairly elegant IMHO, but not flexible or documented enough so it takes a ton of trial and error to setup even WITHOUT additional plugins.
Hope this helps others.
kotlin {
jvm {
compilations {
val main = getByName("main")
tasks {
register<Jar>("fatJar") {
group = "application"
manifest {
attributes["Implementation-Title"] = "Gradle Jar File Example"
attributes["Implementation-Version"] = archiveVersion
attributes["Main-Class"] = "[[mainClassPath]]"
}
archiveBaseName.set("${project.name}-fat")
from(main.output.classesDirs, main.compileDependencyFiles)
with(jar.get() as CopySpec)
}
}
}
}
}
You could use the ShadowJar plugin to build a fat jar:
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
buildscript {
repositories {
mavenCentral()
gradleScriptKotlin()
}
dependencies {
classpath(kotlinModule("gradle-plugin"))
classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
}
}
apply {
plugin("kotlin")
plugin("com.github.johnrengelman.shadow")
}
repositories {
mavenCentral()
}
val shadowJar: ShadowJar by tasks
shadowJar.apply {
manifest.attributes.apply {
put("Implementation-Title", "Gradle Jar File Example")
put("Implementation-Version" version)
put("Main-Class", "com.mkyong.DateUtils")
}
baseName = project.name + "-all"
}
Simply run the task with 'shadowJar'.
NOTE: This assumes you're using GSK 0.7.0 (latest as of 02/13/2017).