permaculture playing cards
The moose likes Java in General and the fly likes custom class loader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "custom class loader" Watch "custom class loader" New topic

custom class loader

william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
I have written a class loader that will load class only form a specified directory.This classLoader is a able to load classes but then i am not able to create an instance of the class that was loaded after i get the Class reference of the loaded class using classref.newInstance() i get class not found exception. What is the way around this ???

Help me!Help you!!!
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
wot classref.newInstance() ?? is supposed tobe
className.newInstance()...isnt it.

SCJP 1.5, SCEA, ICED (287,484,486)
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
Object newInstance()(non static)
Creates a new instance of the class represented by this Class object.
Well this what the api says?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Are you sure you've loaded the class in the first place? It seems extremely odd that you would get a ClassNotFoundException from the newInstance() method, after the Class object has already been created. Especially since ClassNotFoundException is a checked exception which cannot be thrown by newInstance(). I suggest you re-check exactly which line is throwning the exception, and exactly what does the message say. Also check to see if you really have created a Class object - insert a line like
<code><pre> System.out.println("The class object is " + classref);</pre></code>
The other possibility is that newInstance() will not work if there is no public no-argument constructor for your class. You may need to use the getConstructor(Class[]) method of Class to obtain an appropriate Constructor, and then use the newInstance(Object[]) method of Constructor to instantiate the class using any needed initial arguments.

"I'm not back." - Bill Harding, Twister
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
hey thanks Jim Yingst it worked when i added a public no aug constructor.
Now is there a way to actually register my classloader with the jvm such that my class loader gets precedence over the bootstrap classloader?
I agree. Here's the link:
subject: custom class loader
It's not a secret anymore!