问题
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<File> 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});
According to http://www.oracle.com/technetwork/java/javase/9-relnote-issues-3704069.html it looks like this won't work any more with Java 9:
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?
回答1:
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 aURLClassLoader
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.
来源:https://stackoverflow.com/questions/48093609/java-9-how-to-add-further-jar-files-to-classpath-dynamically