Set java.class.path property in runner and loader

#KT-24991 fixed
This commit is contained in:
Ilya Chernikov
2019-08-27 11:23:11 +02:00
parent 08720a3dc6
commit 31c56d7794
6 changed files with 38 additions and 10 deletions
@@ -58,6 +58,7 @@ abstract class AbstractRunner : Runner {
} }
Thread.currentThread().contextClassLoader = classLoader Thread.currentThread().contextClassLoader = classLoader
val savedClasspathProperty = System.setProperty("java.class.path", classpath.joinToString(File.pathSeparator))
try { try {
main.invoke(null, arguments.toTypedArray()) main.invoke(null, arguments.toTypedArray())
@@ -68,6 +69,10 @@ abstract class AbstractRunner : Runner {
catch (e: InvocationTargetException) { catch (e: InvocationTargetException) {
throw e.targetException throw e.targetException
} }
finally {
if (savedClasspathProperty == null) System.clearProperty("java.class.path")
else System.setProperty("java.class.path", savedClasspathProperty)
}
} }
} }
@@ -26,6 +26,7 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@SuppressWarnings("UseOfSystemOutOrSystemErr") @SuppressWarnings("UseOfSystemOutOrSystemErr")
public class Preloader { public class Preloader {
@@ -64,6 +65,8 @@ public class Preloader {
Method mainMethod = mainClass.getMethod("main", String[].class); Method mainMethod = mainClass.getMethod("main", String[].class);
Thread.currentThread().setContextClassLoader(preloaded); 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( Runtime.getRuntime().addShutdownHook(
new Thread(new Runnable() { new Thread(new Runnable() {
@@ -79,8 +82,14 @@ public class Preloader {
}) })
); );
//noinspection SSBasedInspection try {
mainMethod.invoke(0, (Object) options.arguments.toArray(new String[options.arguments.size()])); //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 { private static ClassLoader createClassLoader(Options options) throws MalformedURLException {
+9
View File
@@ -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")
+9 -1
View File
@@ -1,11 +1,19 @@
import java.io.File
object ContextClassLoaderTester { object ContextClassLoaderTester {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
kotlin.test.DefaultAsserter.assertTrue("", true) // this tests that kotlin-test is in the compilation and runtime classpaths kotlin.test.DefaultAsserter.assertTrue("", true) // this tests that kotlin-test is in the compilation and runtime classpaths
val contextClassLoader = Thread.currentThread().getContextClassLoader() val contextClassLoader = Thread.currentThread().getContextClassLoader()
contextClassLoader.loadClass("kotlin.test.DefaultAsserter") // this tests that thread context classloader is set correctly 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")
} }
} }
-3
View File
@@ -1,3 +0,0 @@
package test
println("Hello!")
@@ -79,12 +79,12 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
) )
} }
fun testKotlincJvmSimpleScript() { fun testKotlincJvmScriptWithClassPathFromSysProp() {
runProcess( runProcess(
"kotlinc-jvm", "kotlinc-jvm",
"-script", "-script",
"$testDataDirectory/helloWorld.kts", "$testDataDirectory/classPathPropTest.kts",
expectedStdout = "Hello!\n" expectedStdout = "kotlin-compiler.jar\n"
) )
} }
@@ -102,7 +102,7 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
"kotlin", "kotlin",
"-cp", listOf(tmpdir.path, kotlinTestJar.path).joinToString(File.pathSeparator), "-cp", listOf(tmpdir.path, kotlinTestJar.path).joinToString(File.pathSeparator),
"ContextClassLoaderTester", "ContextClassLoaderTester",
expectedStdout = "ok\n" expectedStdout = "${kotlinTestJar.name}\n"
) )
} }