diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/AgentPremain.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/AgentPremain.kt index 8d0c557ed2..7c26a96b71 100644 --- a/kotlinx-coroutines-core/jvm/src/debug/internal/AgentPremain.kt +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/AgentPremain.kt @@ -15,6 +15,23 @@ import java.security.* @SuppressLint("all") @IgnoreJRERequirement // Never touched on Android internal object AgentPremain { + // This should be the first property to ensure the check happens first! Add new properties only below! + private val dummy = checkIfStdlibIsAvailable() + + /** + * This check ensures that kotlin-stdlib classes are loaded by the time AgentPremain is initialized; + * otherwise the debug session would fail with `java.lang.NoClassDefFoundError: kotlin/Result`. + */ + private fun checkIfStdlibIsAvailable() { + try { + Result.success(42) + } catch (t: Throwable) { + error("kotlinx.coroutines debug agent failed to load.\n" + + "Please ensure that the Kotlin standard library is present in the classpath.\n" + + "Alternatively, you can disable kotlinx.coroutines debug agent by removing `-javaagent=/path/kotlinx-coroutines-core.jar` from your VM arguments.\n" + + t.cause) + } + } private val enableCreationStackTraces = runCatching { System.getProperty("kotlinx.coroutines.debug.enable.creation.stack.trace")?.toBoolean()