Loading classes per filename
Hello.
I am curious if it is possible to load classes only by specifying their filename and how this can be
done?
I am familiar with the 'traditional' method of using URLClassloader and specifying a directory along with the class' fully qualified name (ex; "xy.vz.Myclass").
What I am trying to do is a simple 'plugin-system', where I supply a Interface to people who wants to develop 'plugins' for my application.
The plugins is later placed in a directory which the application scans on startup and loads any *.class or *.jar file located there-in.
I hope I make any sense. I've been trying to find a soluation to this little problem but most searches ends up in the traditional method with the application knowing the full classname and classes/jars being placed in the classpath.
Thank you.
[852 byte] By [
apalssona] at [2007-11-27 4:01:33]

Maybe with getResourceAsStream :
InputStream is = getClass().getResourceAsStream("/" + className);
byte buffer[] = new byte[is.available()];
is.read(buffer);
Class c = defineClass(className, buffer, 0, buffer.length);
Or just Class.forName(className) ?
I guess it depends on you application.
ciola at 2007-7-12 9:06:17 >

> Hello.
>
> I am curious if it is possible to load classes only
> by specifying their filename and how this can be
> done?
Java loads classes by class name. There is no other way.
You could load a class file, parse it (see VM Spec) retrieve the fully qualified class name, and then use that with the class file to load the class.
If it was in a jar you would have to scan the jar using the jar api and extract each class file and do the same as above.
In terms of implementation it would probably be easier just to require that the user updates a configuration file with the name (and optionally a location as well.)