Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-772

Confusing Javadoc on IOUtils#resourceToURL() and other resource* methods

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Trivial
    • Resolution: Fixed
    • 2.11.0
    • 2.12.0
    • Utilities
    • None

    Description

      The Javadoc for IOUtils::resourceToURL (and the String and byte[] variants) says that the "name" parameter is expected to be absolute and is not well defined otherwise. When this is called without a ClassLoader, using an absolute path makes sense as Class::getResource is called against a class that is not the caller's, so a non-absolute path would be looking inside the commons-io package.  But when called with a ClassLoader, instead the ClassLoader::getResource method is called, and using an absolute path with that does not work the same way. 

      For example, both of these work the same for a file sitting at the root of the classpath (src/main/resources in typical Maven/Gradle build):

      • IOUtils.resourceToString("/file.txt");
      • IOUtils.resourceToString("file.txt", getClass().getClassLoader());

      But this does not work:

      • IOUtils.resourceToString("/file.txt", getClass().getClassLoader());

      That behavior is consistent with the explanation in this [accepted StackOverflow answer|https://stackoverflow.com/a/51645482/1270447.]

      I believe the doc on the method variants that take a ClassLoader should call this out.

      Attachments

        Activity

          People

            Unassigned Unassigned
            zman0900 Dan Ziemba
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: