jjg@372: /* jjg@1669: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jjg@372: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@372: * jjg@372: * This code is free software; you can redistribute it and/or modify it jjg@372: * under the terms of the GNU General Public License version 2 only, as jjg@372: * published by the Free Software Foundation. jjg@372: * jjg@372: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@372: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@372: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@372: * version 2 for more details (a copy is included in the LICENSE file that jjg@372: * accompanied this code). jjg@372: * jjg@372: * You should have received a copy of the GNU General Public License version jjg@372: * 2 along with this work; if not, write to the Free Software Foundation, jjg@372: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@372: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@372: */ jjg@372: jjg@372: /* jjg@372: * @test jjg@1669: * @bug 6558476 jjg@1669: * @summary com/sun/tools/javac/Main.compile don't release file handles on return jjg@372: * @run main/othervm -Xmx512m -Xms512m T6558476 jjg@372: */ jjg@372: jjg@372: import java.io.File; jjg@372: import java.io.FileInputStream; jjg@372: import java.io.FileOutputStream; jjg@372: import java.io.IOException; jjg@372: import java.util.Random; jjg@372: jjg@372: import com.sun.tools.javac.Main; jjg@372: jjg@372: public class T6558476 { jjg@372: private static File copyFileTo(File file, File directory) throws IOException { jjg@372: File newFile = new File(directory, file.getName()); jjg@372: FileInputStream fis = null; jjg@372: FileOutputStream fos = null; jjg@372: try { jjg@372: fis = new FileInputStream(file); jjg@372: fos = new FileOutputStream(newFile); jjg@372: byte buff[] = new byte[1024]; jjg@372: int val; jjg@372: while ((val = fis.read(buff)) > 0) jjg@372: fos.write(buff, 0, val); jjg@372: } finally { jjg@372: if (fis != null) jjg@372: fis.close(); jjg@372: if (fos != null) jjg@372: fos.close(); jjg@372: } jjg@372: return newFile; jjg@372: } jjg@372: jjg@372: private static String generateJavaClass(String className) { jjg@372: StringBuffer sb = new StringBuffer(); jjg@372: sb.append("import sun.net.spi.nameservice.dns.DNSNameService;\n"); jjg@372: sb.append("public class "); jjg@372: sb.append(className); jjg@372: sb.append(" {\n"); jjg@372: sb.append(" public void doStuff() {\n"); jjg@372: sb.append(" DNSNameService dns = null;\n"); jjg@372: sb.append(" }\n"); jjg@372: sb.append("}\n"); jjg@372: return sb.toString(); jjg@372: } jjg@372: jjg@372: public static void main(String[] args) throws IOException { jjg@372: File javaHomeDir = new File(System.getProperty("java.home")); jjg@1669: File outputDir = new File("outputDir" + new Random().nextInt(65536)); jjg@372: outputDir.mkdir(); jjg@372: outputDir.deleteOnExit(); jjg@372: jjg@372: File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); jjg@372: File tmpJar = copyFileTo(dnsjarfile, outputDir); jjg@372: String className = "TheJavaFile"; jjg@372: File javaFile = new File(outputDir, className + ".java"); jjg@372: javaFile.deleteOnExit(); jjg@372: FileOutputStream fos = new FileOutputStream(javaFile); jjg@372: fos.write(generateJavaClass(className).getBytes()); jjg@372: fos.close(); jjg@372: jjg@372: int rc = Main.compile(new String[]{"-d", outputDir.getPath(), jjg@372: "-classpath", jjg@372: tmpJar.getPath(), jjg@372: javaFile.getAbsolutePath()}); jjg@372: if (rc != 0) { jjg@372: throw new Error("Couldn't compile the file (exit code=" + rc + ")"); jjg@372: } jjg@372: jjg@372: if (tmpJar.delete()) { jjg@372: System.out.println("jar file successfully deleted"); jjg@372: } else { jjg@372: throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); jjg@372: } jjg@372: } jjg@372: }