Reflectively loading classes from jar file
Hi,
I want to load and then instantiate all classes that lie under a secific package all compressed in a jar file. So, I have an app that uses example.jar :
example.jar - package structure
com
| example
| __ Foo.class
| __ Bar.class
| __ Example.class
| stuff
| __ Util.class
My code within the root app to load the classes from the jar is:
publicclass ReflectionHelper{
public ReflectionHelper(){
}
publicstatic Class getClassInstanceFromJar()throws Exception{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL resource = cl.getResource("com/example");
System.out.println("Package: " + resource);
URLClassLoader ucl =new URLClassLoader(new URL[]{resource}, cl);
Class clazz = ucl.loadClass("com.example.Foo");
System.out.println("Loaded: " + clazz.getName());
return clazz;
}
publicstaticvoid main(String[] args)throws Exception{
ReflectionHelper.getClassInstanceFromJar();
}
}
Everything seem to work fine - the class is loaded normally. The question here is how can I get an Enumeraton or whatever of all resources/URLs that lie under this package so I can load them all without having to say specifically load Foo, load Bar and load Example?
Thanks,
akz

