The code below works beautifully in Tomcat, but the call to getResource(…) returns null in WebSphere 6.1. I’ve tried using both Thread.currentThread().getClassLoader() and MyClass.class.getClassLoader() – both return null.
URL url = null;
ClassLoader cl = MyClass.class.getClassLoader();
LOG.info("Using class's classloader.");
url = cl.getResource("resources/AConfigFile.xml");
if(url == null) {
throw new RuntimeException("The ClassLoader returned null for the URL of the " +
"the XML Document. This is definitely not right.");
}
…and I have also tried this, with no luck…
URL url = null;
url = MyClass.class.getResource("resources/AConfigFile.xml");
if(url == null) {
throw new RuntimeException("The ClassLoader returned null for the URL of the " +
"the XML Document. This is definitely not right.");
}
What’s up with this? How do I properly get a URL for a resource on the classpath?
I’d guess the difference is the way the
ClassLoaders behave. Can you use theClassvariant instead? MyClass.class.getResource()? We useClass.getResourceAsStream()under WebSphere 6.1 all the time.Or perhaps try prefacing your resource path with a leading slash.
Using the
Classvariant, your relative path will look in theresourcessubdirectory under the package ofMyClass. But theClassLoadervariant might not.