jdk/test/sun/misc/ClassLoaderUtil/Test.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 6349735
       
    26  * @summary  Internal API for closing jar files belonging to URLClassLoader instances
       
    27  */
       
    28 
       
    29 import java.io.*;
       
    30 import java.net.*;
       
    31 import java.util.*;
       
    32 
       
    33 public class Test {
       
    34 
       
    35     static File copy (File src, String destDir) throws Exception {
       
    36         FileInputStream fis = new FileInputStream (src);
       
    37         File dest = new File (destDir, src.getName());
       
    38         FileOutputStream fos = new FileOutputStream (dest);
       
    39         byte buf[] = new byte [1024];
       
    40         int c;
       
    41         while ((c=fis.read(buf)) != -1) {
       
    42             fos.write (buf, 0, c);
       
    43         }
       
    44         fis.close();
       
    45         fos.close();
       
    46         return dest;
       
    47     }
       
    48 
       
    49     public static void main(String[] args) throws Exception {
       
    50         String srcPath = System.getProperty("test.src");
       
    51         String destPath = System.getProperty("test.classes");
       
    52         if (destPath == null || "".equals(destPath)) {
       
    53             throw new RuntimeException ("Not running test");
       
    54         }
       
    55         File file = new File (srcPath, "test.jar");
       
    56         file = copy (file, destPath);
       
    57         URL url = file.toURL();
       
    58         URLClassLoader loader = new URLClassLoader (new URL [] {url});
       
    59         Class clazz = Class.forName ("Foo", true,  loader);
       
    60         Object obj = clazz.newInstance();
       
    61         List<String> jarsclosed = new LinkedList<String>();
       
    62         sun.misc.ClassLoaderUtil.releaseLoader (loader, jarsclosed);
       
    63         for (String jar: jarsclosed) {
       
    64             System.out.println ("Successfully closed " + jar);
       
    65         }
       
    66         if (!file.delete()) {
       
    67             throw new RuntimeException ("failed to delete jar file");
       
    68         }
       
    69     }
       
    70 }