Our Java-based application has a tiny "bootloader.jar" and the core application jars. The core application jars might be loaded either from the default (file system) location or from another where a previous application run might have downloaded updated jars. The bootloader runs following code:
final List jars = getJarsToAddToClasspath();
final String mainClassName = getMainClassName();
final URLClassLoader urlClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), ClassLoader.getSystemClassLoader());
final Class> mainClass = urlClassLoader.loadClass(mainClassName);
final Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
mainMethod.invoke(null, new Object[] {args});
Note that Java SE and the JDK do not provide an API for applications or
libraries to dynamically augment the class path at run-time.
Could someone confirm this?
解决方案
I think it should still work. The simplest way to know is to try it. From the same Oracle page:
Code that assumes that ClassLoader::getSytemClassLoader returns a URLClassLoader object will need to be updated.
Your code does not rely on the system classloader being of type URLClassloader. All it does is set it as the delegated parent of the custom classloader. This custom classloader will delegate to it to search for classes and resources regardless of its implementation.