• 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

Classnotfoundexception class.forName()

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to use reflection to get a specific instance of a class. But I have found that both class.forName and ClassLoader.loadClass method throw a classnotfoundexception.
here's the code:


stacktrack


Thanks.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your class path where you are running program has pointed LDAPManager class or its jar.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would probably solve it. I'm wondering why you are loading the class dynamically if you can also load it statically; since you are already using LDAPManager.class, why not simply use LDAPManager.getInstance()? Or is LDAPManager's class loader a custom one (URLClassLoader perhaps), and you want to load the class again? Then I have bad news for you: if the parent class loader could load the class, it already would have, and otherClassInstance == LDAPManager.class (provided it worked). If the parent class loader couldn't load the class before, it still can't now.

So either in the end, otherClassInstance == LDAPManager.class, or there will be a ClassNotFoundException.

To prevent this, either add the JAR file to your class path (in which case you can just use LDAPManager.getInstance()), or create a new class loader to load the class.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic