Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6896088

URLClassLoader.close() apparently not working for JAR URLs on Windows

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 7
    • core-libs
    • b128
    • x86
    • windows_xp
    • Verified

      OPERATING SYSTEM:
      Windows XP. Not reproducible on Solaris.

      JDK VERSION:
      JDK 7 EA b72 was tested. Likely begin with b48, when close() was implemented.

      PROBLEM DESCRIPTION from Licensee:
      Here is the scenario being observed -

      1. We have a jar file containing a class.
      2. We create a URLClassLoader from a URL that points to that jar file,
        employing the JAR prototcol.
      3. We load the class from the jar file via the URLClassLoader.
      4. We close the URLClassLoader via URLClassLoader.close().
      5. We then try to update/delete the jar file, but cannot.

      The result of step 5 is unexpected, and is not what happens if we create a URL using the FILE protocol. If we use the FILE protocol the jar file can be deleted once the loader is closed.

      This problem also only occurs on Windows - the jar file is deleted as expected with both FILE and JAR URL protocols on Solaris and Linux.


      Testcase is attached.
      Example code:

      =======================================================================
      import java.net.*;
      import java.io.*;

      public class TestLoader {
         public static void main(String[] args){
             try {
                 // Create URL using JAR protocol
                 String jarName = ((new File("Sample.jar")).toURI()).toString();
                 URL url = new URL("jar", "", jarName + "!/");

                 // Alternative code using FILE protocol, which works as expected
                 // (jarfile can be deleted after loader is closed)
                 //URL url = ((new File("Sample.jar")).toURI()).toURL();

                 // Create URLClassLoader from the URL
                 URLClassLoader loader = new URLClassLoader(new URL[]{url});
                 Class c = loader.loadClass("Sample");

                 // Close the URLClassLoader so we can delete/update the jar file
                 loader.close();

                 // Now try to delete the jar file
                 File file = new File("Sample.jar");
                 if (file.delete() && !file.exists()) {
                     System.out.println(file.getName()+" File Deleted");
                 } else {
                     System.out.println(file.getName()+" File Not Deleted");
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
      }

            michaelm Michael McMahon
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: