• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading Java Class (Late Binding)  RSS feed

 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somone point me in the right direction (preferably with some sample code) to basically load a class at runtime. I'm thinking that ClassLoader is what I want but from what I saw in the API spec it doesnt seem clear how to do this from a file.

I do *NOT* want to do this with EJB. I want a very simple process by which I can specify a filename and do something like

Animal = new loadClass(new File("someanimal.class"));

I realize I totally oversimplified this but basically I want to load up a a lass by filename at runtime. No EJB, no security just very simple load process at runtime.

If anyone can just point me in the right direction I can get it from there.

Thanks everyone.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Where Qreader is an Interface implemented by all classes that may appear in rcName and all classes are on the application classpath.

Bill
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should be able to do this with reflection:

The Java Tutorials Trail: The Reflection API
Code Example: "Object Reflection: create new instance"
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that if you can't be sure that class files are on the classpath according to the full package.class name convention, things get a LOT more complicated. I just spent a lot of time tearing my hair out with custom classloaders - avoid if at all possible.

Bill
 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, exactly what I needed!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!