|
1 /* |
|
2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6558476 |
|
27 * @summary com/sun/tools/javac/Main.compile don't release file handles on return |
|
28 * @run main/othervm -Xmx512m -Xms512m T6558476 |
|
29 */ |
|
30 |
|
31 import java.io.File; |
|
32 import java.io.FileInputStream; |
|
33 import java.io.FileOutputStream; |
|
34 import java.io.IOException; |
|
35 import java.util.Random; |
|
36 |
|
37 import com.sun.tools.javac.Main; |
|
38 |
|
39 public class T6558476 { |
|
40 private static File copyFileTo(File file, File directory) throws IOException { |
|
41 File newFile = new File(directory, file.getName()); |
|
42 FileInputStream fis = null; |
|
43 FileOutputStream fos = null; |
|
44 try { |
|
45 fis = new FileInputStream(file); |
|
46 fos = new FileOutputStream(newFile); |
|
47 byte buff[] = new byte[1024]; |
|
48 int val; |
|
49 while ((val = fis.read(buff)) > 0) |
|
50 fos.write(buff, 0, val); |
|
51 } finally { |
|
52 if (fis != null) |
|
53 fis.close(); |
|
54 if (fos != null) |
|
55 fos.close(); |
|
56 } |
|
57 return newFile; |
|
58 } |
|
59 |
|
60 private static String generateJavaClass(String className) { |
|
61 StringBuffer sb = new StringBuffer(); |
|
62 sb.append("import sun.net.spi.nameservice.dns.DNSNameService;\n"); |
|
63 sb.append("public class "); |
|
64 sb.append(className); |
|
65 sb.append(" {\n"); |
|
66 sb.append(" public void doStuff() {\n"); |
|
67 sb.append(" DNSNameService dns = null;\n"); |
|
68 sb.append(" }\n"); |
|
69 sb.append("}\n"); |
|
70 return sb.toString(); |
|
71 } |
|
72 |
|
73 public static void main(String[] args) throws IOException { |
|
74 File javaHomeDir = new File(System.getProperty("java.home")); |
|
75 File outputDir = new File("outputDir" + new Random().nextInt(65536)); |
|
76 outputDir.mkdir(); |
|
77 outputDir.deleteOnExit(); |
|
78 |
|
79 File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); |
|
80 File tmpJar = copyFileTo(dnsjarfile, outputDir); |
|
81 String className = "TheJavaFile"; |
|
82 File javaFile = new File(outputDir, className + ".java"); |
|
83 javaFile.deleteOnExit(); |
|
84 FileOutputStream fos = new FileOutputStream(javaFile); |
|
85 fos.write(generateJavaClass(className).getBytes()); |
|
86 fos.close(); |
|
87 |
|
88 int rc = Main.compile(new String[]{"-d", outputDir.getPath(), |
|
89 "-classpath", |
|
90 tmpJar.getPath(), |
|
91 javaFile.getAbsolutePath()}); |
|
92 if (rc != 0) { |
|
93 throw new Error("Couldn't compile the file (exit code=" + rc + ")"); |
|
94 } |
|
95 |
|
96 if (tmpJar.delete()) { |
|
97 System.out.println("jar file successfully deleted"); |
|
98 } else { |
|
99 throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); |
|
100 } |
|
101 } |
|
102 } |