Is it possible to load classes at runtime that are discovered from a directory
I'm working on a command shell program. The idea is it's a blend between the AS/400 and a unix shell with dropdown autocompletes, syntax highlighting, etc. One feature I'd like to implement is a way for users to create their own classes for additional functionality. They would design their classes off of a standard superclass, compile them and then drop them in the classes directory. The program would look in the directory at runtime and load them. Is this possible with my program not knowing the class names before it's compiled?
Re: Is it possible to load classes at runtime that are discovered from a directory
Re: Is it possible to load classes at runtime that are discovered from a directory
Thanks I'm checking it out now.
Re: Is it possible to load classes at runtime that are discovered from a directory
Yes, in some forum software I've written I've added the ability to drop a jar file into a plugin directory and then load that during runtime. You just need to use reflection really.
1. Open the JAR file
2. Loop through all the entries in it
3. If an entry ends with .class, do Class.forName with its fully qualified package name and you're off :)
// Json
Re: Is it possible to load classes at runtime that are discovered from a directory
What if the .class files are NOT on the classpath. Will Class.forName() work?
I think you need your own class loader to read the bytes from the .class file and make a class from it.
Re: Is it possible to load classes at runtime that are discovered from a directory
Quote:
Originally Posted by
Norm
What if the .class files are NOT on the classpath. Will Class.forName() work?
I think you need your own class loader to read the bytes from the .class file and make a class from it.
That's what File Class Loader does.
db
Re: Is it possible to load classes at runtime that are discovered from a directory
Sorry, I missed that your earlier post was a link. Your post was a bit too subtle.
Re: Is it possible to load classes at runtime that are discovered from a directory
I just used the URLClassLoader (Java Platform SE 6)
Code :
final URL[] urls = { new URL("file", null, file.getAbsolutePath()) };
final URLClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
Something like that :D
// Json
Re: Is it possible to load classes at runtime that are discovered from a directory
Quote:
Originally Posted by
Norm
Sorry, I missed that your earlier post was a link. Your post was a bit too subtle.
I'd rather say the (lack of special) formatting for links is (more than) a bit too subtle.
db
Re: Is it possible to load classes at runtime that are discovered from a directory
Quote:
Originally Posted by
Darryl.Burke
I'd rather say the (lack of special) formatting for links is (more than) a bit too subtle.
db
I've raised this as an issue and hopefully we'll be able to resolve that.
// Json
Re: Is it possible to load classes at runtime that are discovered from a directory
Quote:
Originally Posted by
Json
I've raised this as an issue and hopefully we'll be able to resolve that.
// Json
Thank you Json.
db
Re: Is it possible to load classes at runtime that are discovered from a directory
Quote:
Originally Posted by
Json
I've raised this as an issue and hopefully we'll be able to resolve that.
A workaround is to underscore (U) the link text, but it's easy to forget...