diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index 4f0f1f06980..1949b68ee19 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.cli.jvm.config.JvmModulePathRoot import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoot import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots +import org.jetbrains.kotlin.cli.jvm.modules.CoreJrtFileSystem import org.jetbrains.kotlin.cli.jvm.plugins.PluginCliParser import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal import org.jetbrains.kotlin.codegen.CompilationException @@ -404,22 +405,24 @@ class K2JVMCompiler : CLICompiler() { return OK } - if (arguments.jdkHome != null) { + val (jdkHome, classesRoots) = if (arguments.jdkHome != null) { val jdkHome = File(arguments.jdkHome) - configuration.put(JVMConfigurationKeys.JDK_HOME, jdkHome) - val classesRoots = PathUtil.getJdkClassesRoots(jdkHome) - configuration.addJvmClasspathRoots(classesRoots) - messageCollector.report(LOGGING, "Using JDK home directory $jdkHome") + jdkHome to PathUtil.getJdkClassesRoots(jdkHome) + } + else { + File(System.getProperty("java.home")) to PathUtil.getJdkClassesRootsFromCurrentJre() + } + + configuration.put(JVMConfigurationKeys.JDK_HOME, jdkHome) + + if (!CoreJrtFileSystem.isModularJdk(jdkHome)) { + configuration.addJvmClasspathRoots(classesRoots) if (classesRoots.isEmpty()) { messageCollector.report(ERROR, "No class roots are found in the JDK path: $jdkHome") return COMPILATION_ERROR } } - else { - configuration.put(JVMConfigurationKeys.JDK_HOME, File(System.getProperty("java.home"))) - configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRootsFromCurrentJre()) - } } catch (t: Throwable) { MessageCollectorUtil.reportException(messageCollector, t) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/modules/CoreJrtFileSystem.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/modules/CoreJrtFileSystem.kt index e2ac8928428..08e6af1173f 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/modules/CoreJrtFileSystem.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/modules/CoreJrtFileSystem.kt @@ -50,13 +50,17 @@ class CoreJrtFileSystem(private val fileSystem: FileSystem) : DeprecatedVirtualF FileSystems.newFileSystem(rootUri, mapOf("java.home" to jdkHome.absolutePath)) } else { - val jrtFsJar = File(jdkHome, "lib/jrt-fs.jar") - if (!jrtFsJar.exists()) return null - + val jrtFsJar = loadJrtFsJar(jdkHome) ?: return null val classLoader = URLClassLoader(arrayOf(jrtFsJar.toURI().toURL()), null) FileSystems.newFileSystem(rootUri, emptyMap(), classLoader) } return CoreJrtFileSystem(fileSystem) } + + private fun loadJrtFsJar(jdkHome: File): File? = + File(jdkHome, "lib/jrt-fs.jar").takeIf(File::exists) + + fun isModularJdk(jdkHome: File): Boolean = + loadJrtFsJar(jdkHome) != null } } diff --git a/compiler/testData/javaModules/jdkModulesFromNamed/main.txt b/compiler/testData/javaModules/jdkModulesFromNamed/main.txt index 639eeba6f2d..8768f45c1be 100644 --- a/compiler/testData/javaModules/jdkModulesFromNamed/main.txt +++ b/compiler/testData/javaModules/jdkModulesFromNamed/main.txt @@ -1,4 +1,7 @@ compiler/testData/javaModules/jdkModulesFromNamed/main/test.kt:11:24: error: unresolved reference: httpserver val s: com.sun.net.httpserver.HttpServer? = null ^ +compiler/testData/javaModules/jdkModulesFromNamed/main/test.kt:19:20: error: unresolved reference: javafx + val x: com.sun.javafx.tools.ant.AntLog? = null + ^ COMPILATION_ERROR diff --git a/compiler/testData/javaModules/jdkModulesFromNamed/main/test.kt b/compiler/testData/javaModules/jdkModulesFromNamed/main/test.kt index 38a46ba0042..917eeda39e5 100644 --- a/compiler/testData/javaModules/jdkModulesFromNamed/main/test.kt +++ b/compiler/testData/javaModules/jdkModulesFromNamed/main/test.kt @@ -14,4 +14,8 @@ fun main(args: Array) { // Module oracle.desktop val a: com.oracle.awt.AWTUtils? = null println(a) + + // No module, this class is declared in $JDK_9/lib/ant-javafx.jar + val x: com.sun.javafx.tools.ant.AntLog? = null + println(x) } diff --git a/compiler/testData/javaModules/jdkModulesFromUnnamed/main.txt b/compiler/testData/javaModules/jdkModulesFromUnnamed/main.txt index a0aba9318ad..29368c32161 100644 --- a/compiler/testData/javaModules/jdkModulesFromUnnamed/main.txt +++ b/compiler/testData/javaModules/jdkModulesFromUnnamed/main.txt @@ -1 +1,4 @@ -OK \ No newline at end of file +compiler/testData/javaModules/jdkModulesFromUnnamed/main/test.kt:19:33: error: unresolved reference: ant + val x: com.sun.javafx.tools.ant.AntLog? = null + ^ +COMPILATION_ERROR diff --git a/compiler/testData/javaModules/jdkModulesFromUnnamed/main/test.kt b/compiler/testData/javaModules/jdkModulesFromUnnamed/main/test.kt index b9626822910..31eefeed37f 100644 --- a/compiler/testData/javaModules/jdkModulesFromUnnamed/main/test.kt +++ b/compiler/testData/javaModules/jdkModulesFromUnnamed/main/test.kt @@ -14,4 +14,8 @@ fun main(args: Array) { // Module oracle.desktop val a: com.oracle.awt.AWTUtils? = null println(a) + + // No module, this class is declared in $JDK_9/lib/ant-javafx.jar + val x: com.sun.javafx.tools.ant.AntLog? = null + println(x) } diff --git a/compiler/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java b/compiler/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java index be0f5a227d1..1e67cb26812 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java @@ -77,7 +77,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir { .replace("\\", "/") .replace(KotlinCompilerVersion.VERSION, "$VERSION$"); - return normalizedOutputWithoutExitCode + exitCode; + return normalizedOutputWithoutExitCode + exitCode + "\n"; } private void doTest(@NotNull String fileName, @NotNull CLITool compiler) throws Exception {