• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on jars & classloaders

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Our first order of business must be this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic