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.