diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt index 417a94bd21b..3b08420cc50 100644 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt +++ b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt @@ -57,6 +57,8 @@ abstract class AbstractRunner : Runner { ) } + Thread.currentThread().contextClassLoader = classLoader + try { main.invoke(null, arguments.toTypedArray()) } diff --git a/compiler/testData/launcher/contextClassLoaderTester.kt b/compiler/testData/launcher/contextClassLoaderTester.kt new file mode 100644 index 00000000000..0e40af8d0dd --- /dev/null +++ b/compiler/testData/launcher/contextClassLoaderTester.kt @@ -0,0 +1,11 @@ + +object ContextClassLoaderTester { + + @JvmStatic + fun main(args: Array) { + kotlin.test.DefaultAsserter.assertTrue("", true) // this tests that kotlin-test is in the compilation and runtime classpaths + val contextClassLoader = Thread.currentThread().getContextClassLoader() + contextClassLoader.loadClass("kotlin.test.DefaultAsserter") // this tests that thread context classloader is set correctly + println("ok") + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt index f548cc463a5..b81f01b5991 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt @@ -88,6 +88,24 @@ class LauncherScriptTest : TestCaseWithTmpdir() { ) } + fun testKotlinJvmContextClassLoader() { + val kotlinTestJar = File(PathUtil.kotlinPathsForDistDirectory.homePath, "lib/kotlin-test.jar") + assertTrue("kotlin-main-kts.jar not found, run dist task: ${kotlinTestJar.absolutePath}", kotlinTestJar.exists()) + runProcess( + "kotlinc", + "-cp", kotlinTestJar.path, + "$testDataDirectory/contextClassLoaderTester.kt", + "-d", tmpdir.path + ) + + runProcess( + "kotlin", + "-cp", listOf(tmpdir.path, kotlinTestJar.path).joinToString(File.pathSeparator), + "ContextClassLoaderTester", + expectedStdout = "ok\n" + ) + } + fun testKotlincJsSimple() { runProcess( "kotlinc-js",