ajalt / mordant

Multiplatform text styling for Kotlin command-line applications
https://ajalt.github.io/mordant/
Apache License 2.0
982 stars 34 forks source link

`NoClassDefFoundError` when running binaries generated by Compose Multiplatform Gradle plugin #217

Closed jedlimlx closed 1 month ago

jedlimlx commented 1 month ago

I get the error below

Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/management/ManagementFactory
        at com.github.ajalt.mordant.internal.MppInternal_jvmKt.runningInIdeaJavaAgent(MppInternal.jvm.kt:57)
        at com.github.ajalt.mordant.terminal.TerminalDetection.isIntellijRunActionConsole(TerminalDetection.kt:199)
        at com.github.ajalt.mordant.terminal.TerminalDetection.detectTerminal(TerminalDetection.kt:19)
        at com.github.ajalt.mordant.terminal.StdoutTerminalInterface.<init>(StdoutTerminalInterface.kt:17)
        at com.github.ajalt.mordant.terminal.Terminal.<init>(Terminal.kt:52)
        at com.github.ajalt.mordant.terminal.Terminal.<init>(Terminal.kt:41)
        at org.example.MainKt.main(Main.kt:7)
Caused by: java.lang.ClassNotFoundException: java.lang.management.ManagementFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

when using the following Main function

import com.github.ajalt.mordant.terminal.Terminal

fun main() = Terminal().println("test")

and the following build.gradle.kts file

import org.jetbrains.compose.desktop.application.dsl.TargetFormat

plugins {
    kotlin("jvm") version "2.0.0"
    id("org.jetbrains.compose") version "1.6.11"
    id("org.jetbrains.kotlin.plugin.compose") version "2.0.20"
}

group = "org.example"
version = "1.0.0"

repositories {
    mavenCentral()
    google()
}

dependencies {
    implementation("com.github.ajalt.mordant:mordant:2.7.2")
    testImplementation(kotlin("test"))
    implementation("androidx.compose.runtime:runtime:1.6.8")
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(17)
    sourceSets {
        val main by getting
        val test by getting
    }
}

compose.desktop {
    application {
        mainClass = "org.example.MainKt"

        nativeDistributions {
            targetFormats(TargetFormat.Pkg, TargetFormat.Exe, TargetFormat.Deb)
        }
    }
}

The full project is attached. I have tested this on both my Windows 11 computer and a WSL instance running Ubuntu 22.04.1 LTS. mordant_bug.zip

ajalt commented 1 month ago

Thanks for the report. I thought I was already catching that error, but it turns out NoClassDefFoundError is a Throwable, not an Exception.