• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

custom class loader

 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ???
 
Anil Vupputuri
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wot classref.newInstance() ??...it is supposed tobe
className.newInstance()...isnt it.
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object newInstance()(non static)
Creates a new instance of the class represented by this Class object.
Well this what the api says?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic