diff --git a/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java b/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java index 5945636819d..cd92630c416 100644 --- a/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java +++ b/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java @@ -10,6 +10,7 @@ import com.intellij.util.Function; import com.intellij.util.Processor; import jet.modules.IModuleBuilder; import jet.modules.IModuleSetBuilder; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetCoreEnvironment; import org.jetbrains.jet.codegen.ClassFileFactory; import org.jetbrains.jet.codegen.GeneratedClassLoader; @@ -154,7 +155,12 @@ public class CompileEnvironment { final String directory = new File(moduleFile).getParent(); for (IModuleBuilder moduleBuilder : moduleSetBuilder.getModules()) { ClassFileFactory moduleFactory = compileModule(moduleBuilder, directory); - writeToJar(moduleFactory, new File(directory, moduleBuilder.getModuleName() + ".jar").getPath(), null, true); + final String path = new File(directory, moduleBuilder.getModuleName() + ".jar").getPath(); + try { + writeToJar(moduleFactory, new FileOutputStream(path), null, true); + } catch (FileNotFoundException e) { + throw new CompileEnvironmentException("Invalid jar path " + path, e); + } } } @@ -232,7 +238,7 @@ public class CompileEnvironment { return new File(PathManager.getResourceRoot(CompileEnvironment.class, "/org/jetbrains/jet/compiler/CompileEnvironment.class")).getParentFile().getParentFile().getParent(); } - public static void writeToJar(ClassFileFactory factory, String jar, String mainClass, boolean includeRuntime) { + public static void writeToJar(ClassFileFactory factory, final OutputStream fos, @Nullable String mainClass, boolean includeRuntime) { try { Manifest manifest = new Manifest(); final Attributes mainAttributes = manifest.getMainAttributes(); @@ -241,7 +247,6 @@ public class CompileEnvironment { if (mainClass != null) { mainAttributes.putValue("Main-Class", mainClass); } - FileOutputStream fos = new FileOutputStream(jar); JarOutputStream stream = new JarOutputStream(fos, manifest); try { for (String file : factory.files()) { @@ -327,7 +332,11 @@ public class CompileEnvironment { ClassFileFactory factory = session.generate(); if (jar != null) { - writeToJar(factory, jar, mainClass, true); + try { + writeToJar(factory, new FileOutputStream(jar), mainClass, true); + } catch (FileNotFoundException e) { + throw new CompileEnvironmentException("Invalid jar path " + jar, e); + } } else if (outputDir != null) { writeToOutputDirectory(factory, outputDir); diff --git a/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java b/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java index da76a194917..4b01efe9efd 100644 --- a/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java +++ b/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java @@ -6,7 +6,14 @@ import junit.framework.TestCase; import org.jetbrains.jet.codegen.ClassFileFactory; import org.jetbrains.jet.parsing.JetParsingTest; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; /** * @author yole @@ -25,7 +32,7 @@ public class CompileEnvironmentTest extends TestCase { super.tearDown(); } - public void testSmoke() { + public void testSmoke() throws IOException { final File activeRtJar = CompileEnvironment.findRtJar(true); environment.setJavaRuntime(activeRtJar); environment.initializeKotlinRuntime(); @@ -36,5 +43,23 @@ public class CompileEnvironmentTest extends TestCase { final ClassFileFactory factory = environment.compileModule(moduleBuilder, testDataDir); assertNotNull(factory); assertNotNull(factory.asBytes("Smoke/namespace.class")); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + CompileEnvironment.writeToJar(factory, baos, null, false); + JarInputStream is = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); + final List entries = listEntries(is); + assertTrue(entries.contains("Smoke/namespace.class")); + } + + private List listEntries(JarInputStream is) throws IOException { + List entries = new ArrayList(); + while (true) { + final JarEntry jarEntry = is.getNextJarEntry(); + if (jarEntry == null) { + break; + } + entries.add(jarEntry.getName()); + } + return entries; } }