468,483 Members | 2,598 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,483 developers. It's quick & easy.

Dynamic class loading

I need to be able to select a class when my app is running, then load it
and create an instance. I know this means I need some sort of class
loader but I'm getting lost.

Classes to be loaded can be anywhere in the directory tree but the classes
(usually) don't belong to a package. If they are in the same directory
that I start the app from, things work, otherwise they don't.

Any ideas, help or refs?

Tony
Jul 17 '05 #1
2 3219
"Tony Burrows" <to**@tonyburrows.com> wrote in message
news:pa****************************@tonyburrows.co m...
| I need to be able to select a class when my app is running, then load it
| and create an instance. I know this means I need some sort of class
| loader but I'm getting lost.
|
| Classes to be loaded can be anywhere in the directory tree but the classes
| (usually) don't belong to a package. If they are in the same directory
| that I start the app from, things work, otherwise they don't.
|
| Any ideas, help or refs?
|
| Tony

Class myClass = Class.forName("com.mypackage.MyClass");
MyType instance = (MyType) myClass.newInstance();

--
-P
"Much as some folks hate to lose their way, me, I pray to God that I will."

Jul 17 '05 #2
You are correct in your assumption of needing to make a classloader.
This isn't as difficult as it sounds, and the API documentation at
http://java.sun.com/j2se/1.4.2/docs/api/ for the ClassLoader class it
very thorough in explaining how to implement one (I had to do this
myself a few months back).

Note that as far as I've been able to determine, this does NOT allow you
to load DLL files not within the OS's classpath (so JNI's are pretty
much out).

Tony Burrows wrote:
I need to be able to select a class when my app is running, then load it
and create an instance. I know this means I need some sort of class
loader but I'm getting lost.

Classes to be loaded can be anywhere in the directory tree but the classes
(usually) don't belong to a package. If they are in the same directory
that I start the app from, things work, otherwise they don't.

Any ideas, help or refs?

Tony

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Parag Joshi | last post: by
17 posts views Thread by Aguilar, James | last post: by
4 posts views Thread by Yan Wang | last post: by
reply views Thread by John Allman | last post: by
9 posts views Thread by pbd22 | last post: by
11 posts views Thread by digz | last post: by
5 posts views Thread by bearophileHUGS | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.