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 3b08420cc50..db3ed63c54d 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 @@ -58,6 +58,7 @@ abstract class AbstractRunner : Runner { } Thread.currentThread().contextClassLoader = classLoader + val savedClasspathProperty = System.setProperty("java.class.path", classpath.joinToString(File.pathSeparator)) try { main.invoke(null, arguments.toTypedArray()) @@ -68,6 +69,10 @@ abstract class AbstractRunner : Runner { catch (e: InvocationTargetException) { throw e.targetException } + finally { + if (savedClasspathProperty == null) System.clearProperty("java.class.path") + else System.setProperty("java.class.path", savedClasspathProperty) + } } } diff --git a/compiler/preloader/src/org/jetbrains/kotlin/preloading/Preloader.java b/compiler/preloader/src/org/jetbrains/kotlin/preloading/Preloader.java index 35378ad709f..5def9cef059 100644 --- a/compiler/preloader/src/org/jetbrains/kotlin/preloading/Preloader.java +++ b/compiler/preloader/src/org/jetbrains/kotlin/preloading/Preloader.java @@ -26,6 +26,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.*; +import java.util.stream.Collectors; @SuppressWarnings("UseOfSystemOutOrSystemErr") public class Preloader { @@ -64,6 +65,8 @@ public class Preloader { Method mainMethod = mainClass.getMethod("main", String[].class); Thread.currentThread().setContextClassLoader(preloaded); + String classPathString = options.classpath.stream().map(File::getPath).collect(Collectors.joining(File.pathSeparator)); + String savedClasspathProperty = System.setProperty("java.class.path", classPathString); Runtime.getRuntime().addShutdownHook( new Thread(new Runnable() { @@ -79,8 +82,14 @@ public class Preloader { }) ); - //noinspection SSBasedInspection - mainMethod.invoke(0, (Object) options.arguments.toArray(new String[options.arguments.size()])); + try { + //noinspection SSBasedInspection + mainMethod.invoke(0, (Object) options.arguments.toArray(new String[options.arguments.size()])); + } + finally { + if (savedClasspathProperty == null) System.clearProperty("java.class.path"); + else System.setProperty("java.class.path", savedClasspathProperty); + } } private static ClassLoader createClassLoader(Options options) throws MalformedURLException { diff --git a/compiler/testData/launcher/classPathPropTest.kts b/compiler/testData/launcher/classPathPropTest.kts new file mode 100644 index 00000000000..35c5ff46695 --- /dev/null +++ b/compiler/testData/launcher/classPathPropTest.kts @@ -0,0 +1,9 @@ +package test + +import java.io.File + +val classPathFromProp = System.getProperty("java.class.path") + +val jarFromProps = classPathFromProp.split(File.pathSeparator).firstOrNull { it.contains("kotlin-compiler") } + +println(jarFromProps?.let { File(it).name } ?: "kotlin-compiler not found in the java.class.path property: $classPathFromProp") diff --git a/compiler/testData/launcher/contextClassLoaderTester.kt b/compiler/testData/launcher/contextClassLoaderTester.kt index 0e40af8d0dd..89afd841606 100644 --- a/compiler/testData/launcher/contextClassLoaderTester.kt +++ b/compiler/testData/launcher/contextClassLoaderTester.kt @@ -1,11 +1,19 @@ +import java.io.File + 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") + + val classPathFromProp = System.getProperty("java.class.path") + + val jarFromProps = classPathFromProp.split(File.pathSeparator).firstOrNull { it.contains("kotlin-test") } + + println(jarFromProps?.let { File(it).name } ?: "kotlin-test.jar not found in the java.class.path property: $classPathFromProp") } } diff --git a/compiler/testData/launcher/helloWorld.kts b/compiler/testData/launcher/helloWorld.kts deleted file mode 100644 index 436aded6c6c..00000000000 --- a/compiler/testData/launcher/helloWorld.kts +++ /dev/null @@ -1,3 +0,0 @@ -package test - -println("Hello!") diff --git a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt index b81f01b5991..1d09a87e6e3 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt @@ -79,12 +79,12 @@ class LauncherScriptTest : TestCaseWithTmpdir() { ) } - fun testKotlincJvmSimpleScript() { + fun testKotlincJvmScriptWithClassPathFromSysProp() { runProcess( "kotlinc-jvm", "-script", - "$testDataDirectory/helloWorld.kts", - expectedStdout = "Hello!\n" + "$testDataDirectory/classPathPropTest.kts", + expectedStdout = "kotlin-compiler.jar\n" ) } @@ -102,7 +102,7 @@ class LauncherScriptTest : TestCaseWithTmpdir() { "kotlin", "-cp", listOf(tmpdir.path, kotlinTestJar.path).joinToString(File.pathSeparator), "ContextClassLoaderTester", - expectedStdout = "ok\n" + expectedStdout = "${kotlinTestJar.name}\n" ) }