• Post Reply Bookmark Topic Watch Topic
  • New Topic

question on jars & classloaders  RSS feed

 
nikki lorenzo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've this A.jar inside WEB-INF/lib who needs to locate some class in B.jar (also in WEB-INF/lib) using ClassLoader.getSystemResource("com/sample/ClassB.class"). Problem is, this always returns null. How do i get this working?

Help needed desperately. Thanks & Regards.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you want to get the class as stream ? why dont you just use Class.forName("da.di.Dooo");
?

if you really need it as a resource, try with a leading "/".

p
 
nikki lorenzo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But that doesn't work either. Also, what i need is the location of the file and not the file itself. Thus, the use of ClassLoader.getSystemResource which returns a URL.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassLoaders are tricky to get right.

The system ClassLoader only works with the classes on the classpath. Thus, you cannot use it in many Web applications, because your classes are loaded via custom ClassLoaders.

You could get the current class' ClassLoader via this.getClass().getClassLoader(). Or you could get the thread context ClassLoader via Thread.getCurrentThread().getContextClassLoader(). Having got your ClassLoader, you can use it to get another Class or resource. You may find that you can use findResource() on the ClassLoader, but that will only have protected access, unless the custom ClassLoader widened it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!