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

ResourceBundle.getBundle can leave file open

    Details

    • Subcomponent:
    • Introduced In Version:
      9
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      A DESCRIPTION OF THE PROBLEM :
      ResourceBundle.getBundle leaves the file open if the bundle is inside a jar file. This means the jar file cannot be deleted. I have a simple test program that demonstrates the problem -- it works fine with Java 8, but fails with Java 11 (and I tried early access 13).

      REGRESSION : Last worked in version 8u202

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the demo program with java 8 and with java 11, observe that the jar file is deleted ok when run with java 8, but cannot be deleted with java 11.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect the jar file to be deleted.
      ACTUAL -
      The jar file is not deleted (because it is left open).

      ---------- BEGIN SOURCE ----------
      Here's the source, but it requires (1) commons-io just for doing a simple file copy, (2) a mysql jdbc driver jar file.
      import java.io.File;
      import java.io.IOException;
      import java.net.URL;
      import java.net.URLClassLoader;
      import java.util.Locale;
      import java.util.ResourceBundle;

      import org.apache.commons.io.FileUtils;

      /**
       * This demonstrates a bug in Java 11 ResourceBundle.
       * If you run this program with Java 8, the file is indeed deleted.
       * If you run with Java 11, the file is not deleted. Ditto early release of Java 13.
       */
      public class ResourceBundleBug {
          private static final String FILE = "mysql-connector-java-5.1.35-bin.jar";

          public static void main(String[] args) throws IOException {
              FileUtils.copyFileToDirectory(new File("res/" + FILE), FileUtils.getTempDirectory());
              URL url = new URL("file:" + FileUtils.getTempDirectoryPath() + "/" + FILE);
              URLClassLoader cl = new URLClassLoader(new URL[] {url}, null);
              ResourceBundle rb = ResourceBundle.getBundle("com.mysql.jdbc.LocalizedErrorMessages", Locale.US, cl);
              cl.close();
              File file = new File(FileUtils.getTempDirectoryPath() + "/" + FILE);
              System.out.println(file + " exists: " + file.exists());
              boolean deleted = file.delete();
              System.out.println("file was deleted: " + deleted);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      could not find one, would love to have one!

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              naoto Naoto Sato
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: