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
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)
}
}
}
@@ -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 {
+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 {
@JvmStatic
fun main(args: Array<String>) {
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")
}
}
-3
View File
@@ -1,3 +0,0 @@
package test
println("Hello!")
@@ -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"
)
}