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

Closing a URLClassLoader instance affect another URLClassLoader instance when getResourceAsStream() is being used

XMLWordPrintable

    • x86_64
    • linux

      ADDITIONAL SYSTEM INFORMATION :
      lsb_release -a
      LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
      Distributor ID: CentOS
      Description: CentOS Linux release 7.3.1611 (Core)
      Release: 7.3.1611
      Codename: Core

      The issue can be reproduced with the following versions of OpenJDK:
      openjdk version "1.8.0_171"
      OpenJDK Runtime Environment (build 1.8.0_171-b10)
      OpenJDK 64-Bit Server VM (build 25.171-b10, mixed mode)

      openjdk version "9"
      OpenJDK Runtime Environment (build 9+181)
      OpenJDK 64-Bit Server VM (build 9+181, mixed mode)

      openjdk version "10.0.1" 2018-04-17
      OpenJDK Runtime Environment (build 10.0.1+10)
      OpenJDK 64-Bit Server VM (build 10.0.1+10, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      This is the exact same issue as JDK-8155607 which is marked as resolved but the issue can still be reproduced.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See JDK-8155607


      ---------- BEGIN SOURCE ----------
      //
      // This is the same test code that is attached to the original bug JDK-8155607
      //
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.InputStream;
      import java.net.URL;
      import java.net.URLClassLoader;
      import java.util.jar.JarEntry;
      import java.util.jar.JarOutputStream;

      public class SimpleTest {

        public static void main(String[] args) throws Exception {
          // Generate a jar file with a file inside
          File jarFile = File.createTempFile("test", ".jar");
          try {
            try (JarOutputStream jarOutput = new JarOutputStream(new FileOutputStream(jarFile))) {
              jarOutput.putNextEntry(new JarEntry("file.txt"));
              jarOutput.write("Hello World".getBytes("UTF-8"));
              jarOutput.closeEntry();
            }

            // Define two different instances of URLClassLoader from the same set of urls.
            URL[] urls = new URL[]{ jarFile.toURI().toURL() };
            URLClassLoader cl1 = new URLClassLoader(urls, null);
            URLClassLoader cl2 = new URLClassLoader(urls, null);

            // Open a resource stream from the first CL
            try (InputStream is = cl1.getResourceAsStream("file.txt")) {
              System.out.println("First stream: " + is);
            }

            // Open another resource stream from the second CL, with the same resource name
            try (InputStream is = cl2.getResourceAsStream("file.txt")) {
              System.out.println("Second stream: " + is);
              while (is.read() >= 0) {
                // While reading, close the first CL, the next is.read() will throw a "java.io.IOException: Stream closed"
                cl1.close();
              }
            }

            cl2.close();
          } finally {
            jarFile.delete();
          }
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No known workaround

      FREQUENCY : always


            jpai Jaikiran Pai
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: