Conceptually, what am I doing when I’m loading a new Jar? Is URLClassloader the only choice? How should I form those URLs to point to a subdirectory containing more jars.
If anyone is feeling super-generous, some demonstration code to do the following would be really helpful (let’s assume “jars/A.jar” contains “myClass” which we want to instantiate):
- load some jar from a subdirectory
- return a particular class from it
- instantiate that class
Conceptually, you are not loading a new JAR. Rather, you are defining a class loader which will load code and other resources from the JAR file on demand.
In theory, you could implement your own subclass of
ClassLoader, but that’s not necessary for what you are trying to do.This is the nub of your problem I think. The
URLClassLoaderconstructors interpret theURL[]argument as follows:In the first case of the quoted text above, the directory is assumed to be the root of tree containing resources to be loaded.
But I gather you are trying to set up a class loader that will load from all JAR files in a given directory. To do that, you need to:
Fileobjects for any JAR files.URLinstances.File, useFile.toURL()to create aURLand add to the array. (UsingFile.toURL()means that you will get kosher “file:” URLs for the JAR files that will work on your platform.)URLClassLoaderusing theURLarray.