diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java index 852babf8b84..5de010cc739 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java @@ -200,6 +200,7 @@ public class CompileEnvironmentUtil { } } + // TODO: includeRuntime should be not a flag but a path to runtime public static void writeToJar(ClassFileFactory factory, final OutputStream fos, @Nullable FqName mainClass, boolean includeRuntime) { try { Manifest manifest = new Manifest(); @@ -210,19 +211,14 @@ public class CompileEnvironmentUtil { mainAttributes.putValue("Main-Class", mainClass.getFqName()); } JarOutputStream stream = new JarOutputStream(fos, manifest); - try { - for (String file : factory.files()) { - stream.putNextEntry(new JarEntry(file)); - stream.write(factory.asBytes(file)); - } - if (includeRuntime) { - writeRuntimeToJar(stream); - } + for (String file : factory.files()) { + stream.putNextEntry(new JarEntry(file)); + stream.write(factory.asBytes(file)); } - finally { - stream.close(); - fos.close(); + if (includeRuntime) { + writeRuntimeToJar(stream); } + stream.close(); } catch (IOException e) { throw new CompileEnvironmentException("Failed to generate jar file", e); diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java index be92f662f65..8835341d722 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java @@ -43,11 +43,13 @@ import org.jetbrains.jet.lang.resolve.java.JvmAbi; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.plugin.JetLanguage; import org.jetbrains.jet.plugin.JetMainDetector; +import org.jetbrains.jet.utils.ExceptionUtils; import org.jetbrains.jet.utils.Progress; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.Collections; @@ -107,12 +109,23 @@ public class KotlinToJVMBytecodeCompiler { } else { String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath(); + FileOutputStream outputStream = null; try { - CompileEnvironmentUtil.writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime); + outputStream = new FileOutputStream(path); + CompileEnvironmentUtil.writeToJar(moduleFactory, outputStream, null, jarRuntime); + outputStream.close(); } catch (FileNotFoundException e) { throw new CompileEnvironmentException("Invalid jar path " + path, e); } + catch (IOException e) { + throw ExceptionUtils.rethrow(e); + } + finally { + try { + outputStream.close(); + } catch (Throwable e) {} + } } } return true; @@ -142,12 +155,23 @@ public class KotlinToJVMBytecodeCompiler { try { ClassFileFactory factory = generationState.getFactory(); if (jar != null) { + FileOutputStream os = null; try { + os = new FileOutputStream(jar); CompileEnvironmentUtil.writeToJar(factory, new FileOutputStream(jar), mainClass, includeRuntime); + os.close(); } catch (FileNotFoundException e) { throw new CompileEnvironmentException("Invalid jar path " + jar, e); } + catch (IOException e) { + throw ExceptionUtils.rethrow(e); + } + finally { + try { + os.close(); + } catch (Throwable e) {} + } } else if (outputDir != null) { CompileEnvironmentUtil.writeToOutputDirectory(factory, outputDir);