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

Better documentation for Class#getResource(String), etc

    XMLWordPrintable

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      Note: this is actually regarding all Java Releases and OS independent.

      Issues at:
      https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#getResource(java.lang.String)
      https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getResource(java.lang.String)

      These methods are lacking explanation regarding their parameter "name" and needs improvement.

      1) It should be clearer, that Class#getResource(String) will search relative to the package, unless an absolute path is passed, and for ClassLoader#getResource(String) expects an absolute path.
      2) For ClassLoader#getResource(String) should also be noted, that a leading '/' will actually cause problems.

      Example:
      Absolute resource path: /org/example/resource.res

      Accessing the resource via org.ExampleClass:
      ExampleClass.class.getResource("example/resource.res"); // works as expected
      ExampleClass.class.getResource("/org/example/resource.res"); // works as expected

      Accessing the resouce via org.ExampleClass.class.getClassLoader():
      ExampleClass.class.getClassLoader().getResource("org/example/resource.res"); // works as expected
      ExampleClass.class.getClassLoader().getResource("/org/example/resource.res"); // breaks, unexpected behavior


        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            clanger Christoph Langer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: