1 /*
2  * Copyright (c) 2004, 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 5071352
27  * @summary Make sure compiler closes all open files on Windows
28  * @author Martin Buchholz
29  *
30  * @run main/othervm -Xms200m -Xmx200m CompileClose
31  */
32 
33 // -Xms120m is sufficient to inhibit a gc during a compile.
34 // -Xms200m leaves room for expansion and was used by the customer.
35 
36 import java.io.*;
37 
38 public class CompileClose {
writeFile(String f, String contents)39     static void writeFile(String f, String contents) throws IOException {
40         PrintStream s = new PrintStream(new FileOutputStream(f));
41         s.println(contents);
42         s.close();
43     }
44 
rm(String filename)45     static void rm(String filename) throws Exception {
46         File f = new File(filename);
47         f.delete();
48         if (f.exists())
49             throw new Exception(filename + ": couldn't remove");
50     }
51 
clean()52     static void clean() throws Exception {
53         rm("tmpCompileClose.java");
54         rm("tmpCompileClose.class");
55         rm("tmpCompileClose.jar");
56     }
57 
main(String args[])58     public static void main(String args[]) throws Exception {
59         try {
60             clean();
61             main1();
62         } finally {
63             clean();
64         }
65     }
66 
main1()67     static void main1() throws Exception {
68         writeFile("tmpCompileClose.java",
69                   "public class tmpCompileClose {}");
70         // Any old jar file will do
71         SameJVM.jar("cf", "tmpCompileClose.jar", "tmpCompileClose.java");
72         System.gc(); // Inhibit gc during next compile
73         SameJVM.javac("-cp", "tmpCompileClose.jar", "tmpCompileClose.java");
74         // The following rm is the actual test!
75         rm("tmpCompileClose.jar");
76     }
77 }
78