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

URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before

    XMLWordPrintable

Details

    • b128
    • generic
    • generic
    • Verified

    Description

      The specification for URLClassLoader.close() states:
      "In the case of jar: and file: URLs, it also closes any class files, or JAR files that were opened by it."

      If a resource was got(by getResourceAsStream(...)) from resource file or jar file, this file couldn't be deleted on Windows after the call of URLClassLoader.close(). Hence the method close() doesn't close class/jar files.

      Please see the following test:
      ----------------------------------------------
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.net.URL;
      import java.net.URLClassLoader;

      public class UCL_minitest {
          public static void main(String[] args) throws IOException {
              final String resName = "resource.res";
              File resFile = new File(resName);
              FileOutputStream fos = new FileOutputStream(resName);
              fos.write(new byte[]{0, 1, 2, 3});
              fos.close();

              URL parentURL = resFile.getCanonicalFile().getParentFile().toURI().toURL();
              URLClassLoader loader = new URLClassLoader(new URL[] { parentURL });
              System.out.println("resource: " + loader.getResourceAsStream(resName));
              loader.close();
              System.out.println("file is successfully deleted: " + resFile.delete());
          }
      }
      ----------------------------------------------

      It's output on Solaris:
      resource: java.io.BufferedInputStream@1eed786
      file is successfully deleted: true

      on Windows:
      resource: java.io.BufferedInputStream@c3c749
      file is successfully deleted: false

      Attachments

        Issue Links

          Activity

            People

              michaelm Michael McMahon
              dmiltsov Dmitry Miltsov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: