From 00667b14e730fc65d90861816c7299423c4effc7 Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Mon, 23 Jan 2012 21:50:16 +0400 Subject: [PATCH] Simplify rt.jar discovery --- .../org/jetbrains/jet/cli/KotlinCompiler.java | 2 +- .../jet/compiler/CompileEnvironment.java | 73 ++----------------- .../org/jetbrains/jet/JetLiteFixture.java | 2 +- .../jet/compiler/CompileEnvironmentTest.java | 2 +- 4 files changed, 10 insertions(+), 69 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java index 2f43457dc3d..2e12a772af1 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java @@ -62,7 +62,7 @@ public class KotlinCompiler { CompileEnvironment environment = new CompileEnvironment(); try { - environment.setJavaRuntime(CompileEnvironment.findRtJar(true)); + environment.setJavaRuntime(CompileEnvironment.findRtJar()); if (!environment.initializeKotlinRuntime()) { System.err.println("No Kotlin runtime library found"); System.exit(1); diff --git a/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java b/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java index 17193241a1f..94b9fc5b889 100644 --- a/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java +++ b/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java @@ -1,11 +1,8 @@ package org.jetbrains.jet.compiler; import com.intellij.openapi.Disposable; -import com.intellij.openapi.application.PathManager; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.util.Function; import com.intellij.util.Processor; import jet.modules.AllModules; import jet.modules.Module; @@ -20,7 +17,6 @@ import org.jetbrains.jet.plugin.JetMainDetector; import java.io.*; import java.lang.reflect.Method; import java.net.URL; -import java.net.URLClassLoader; import java.util.List; import java.util.jar.*; @@ -93,34 +89,18 @@ public class CompileEnvironment { myEnvironment.addToClasspath(rtJarPath); } - public static File findRtJar(boolean failOnError) { - String javaHome = System.getenv("JAVA_HOME"); - if (javaHome == null) { - javaHome = System.getProperty("java.home"); - if ("jre".equals(new File(javaHome).getName())) { - javaHome = new File(javaHome).getParent(); - } + public static File findRtJar() { + String javaHome = System.getProperty("java.home"); + if ("jre".equals(new File(javaHome).getName())) { + javaHome = new File(javaHome).getParent(); } - File rtJar; - if (javaHome == null) { - rtJar = findActiveRtJar(failOnError); + File rtJar = findRtJar(javaHome); - if(rtJar == null && failOnError) { - throw new CompileEnvironmentException("JAVA_HOME environment variable needs to be defined"); - } - } - else { - rtJar = findRtJar(javaHome); + if (rtJar == null || !rtJar.exists()) { + throw new CompileEnvironmentException("No JDK rt.jar found under" + javaHome); } - if ((rtJar == null || !rtJar.exists()) && failOnError) { - rtJar = findActiveRtJar(failOnError); - - if ((rtJar == null || !rtJar.exists())) { - throw new CompileEnvironmentException("No JDK rt.jar found under JAVA_HOME=" + javaHome); - } - } return rtJar; } @@ -137,42 +117,7 @@ public class CompileEnvironment { return null; } - public static File findActiveRtJar(boolean failOnError) { - ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); - if (systemClassLoader instanceof URLClassLoader) { - URLClassLoader loader = (URLClassLoader) systemClassLoader; - for (URL url: loader.getURLs()) { - if("file".equals(url.getProtocol())) { - if(url.getFile().endsWith("/lib/rt.jar")) { - return new File(url.getFile()); - } - if(url.getFile().endsWith("/Classes/classes.jar")) { - return new File(url.getFile()).getAbsoluteFile(); - } - } - } - if (failOnError) { - throw new CompileEnvironmentException("Could not find rt.jar in system class loader: " + StringUtil.join(loader.getURLs(), new Function() { - @Override - public String fun(URL url) { - return url.toString() + "\n"; - } - }, ", ")); - } - } - else if (failOnError) { - throw new CompileEnvironmentException("System class loader is not an URLClassLoader: " + systemClassLoader); - } - return null; - } - public void compileModuleScript(String moduleFile, String jarPath, boolean jarRuntime) { - try { - System.out.println("module file text: " + FileUtil.loadFile(new File(moduleFile))); - } catch (Exception e) { - e.printStackTrace(); - } - final List modules = loadModuleScript(moduleFile); if (modules == null) { @@ -246,10 +191,6 @@ public class CompileEnvironment { return moduleCompileSession.generate(); } - private static String getHomeDirectory() { - return new File(PathManager.getResourceRoot(CompileEnvironment.class, "/org/jetbrains/jet/compiler/CompileEnvironment.class")).getParentFile().getParentFile().getParent(); - } - public static void writeToJar(ClassFileFactory factory, final OutputStream fos, @Nullable String mainClass, boolean includeRuntime) { try { Manifest manifest = new Manifest(); diff --git a/compiler/tests/org/jetbrains/jet/JetLiteFixture.java b/compiler/tests/org/jetbrains/jet/JetLiteFixture.java index a1a18765434..2e0eddb18a7 100644 --- a/compiler/tests/org/jetbrains/jet/JetLiteFixture.java +++ b/compiler/tests/org/jetbrains/jet/JetLiteFixture.java @@ -59,7 +59,7 @@ public abstract class JetLiteFixture extends UsefulTestCase { protected void createEnvironmentWithFullJdk() { myEnvironment = new JetCoreEnvironment(getTestRootDisposable()); - final File rtJar = CompileEnvironment.findRtJar(true); + final File rtJar = CompileEnvironment.findRtJar(); myEnvironment.addToClasspath(rtJar); } diff --git a/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java b/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java index da1222b561c..e6bae3ff69a 100644 --- a/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java +++ b/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java @@ -32,7 +32,7 @@ public class CompileEnvironmentTest extends TestCase { } public void _testSmoke() throws IOException { - final File activeRtJar = CompileEnvironment.findRtJar(true); + final File activeRtJar = CompileEnvironment.findRtJar(); environment.setJavaRuntime(activeRtJar); environment.initializeKotlinRuntime(); final String testDataDir = JetParsingTest.getTestDataDir() + "/compiler/smoke/";