Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

URLClassLoader throws java.lang.ClassNotFoundException

 
Ranch Hand
Posts: 40
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to dynamically loading class files. This is my code:


classIndexer.getInterfaceFile(field.getType().getName()) returns a File object that contains the file path C:\Users\Christian\Documents\GitHub\KBSE-CDI\target\classes\de\ksw\kbse\main\BarImpl.class. Then I split this path into file name and it's folder path.

But for some reason I get a ClassNotFoundException. Why is that so?

Thanks!
--------------------------------------------------------------------------------------
edit: OK, I forgot to remove the .class before invoking loadClass(). Now I have this:


But I get a different exception:

Exception in thread "main" java.lang.NoClassDefFoundError: BarImpl (wrong name: de/ksw/kbse/main/BarImpl)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at de.ksw.kbse.di.CDIC.simpleInjection(CDIC.java:45)
at de.ksw.kbse.di.CDIC.inject(CDIC.java:108)
at de.ksw.kbse.di.CDIC.init(CDIC.java:95)
at de.ksw.kbse.main.Main.main(Main.java:9)



Any ideas? I'll do some googleing.
 
Rancher
Posts: 3776
34
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exception in thread "main" java.lang.NoClassDefFoundError: BarImpl (wrong name: de/ksw/kbse/main/BarImpl)  


It looks like the class is in a package.  Are you using the full class name (includes package)?
 
Christian Wansart
Ranch Hand
Posts: 40
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Exception in thread "main" java.lang.NoClassDefFoundError: BarImpl (wrong name: de/ksw/kbse/main/BarImpl)  


It looks like the class is in a package.  Are you using the full class name (includes package)?


Yes. I guess I have to save the root path as well. I hoped it would work that way too.
 
Christian Wansart
Ranch Hand
Posts: 40
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks for the pointer. Now my classIndexer.getInterfaceFile(field.getType().getName()) method returns a ClassInfo (my own class that contains the name, path and file) instead of a File object. Now I can load my class files. Thanks!
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic