Java 9: how to add further jar files to classpath dynamically [duplicate]

柔情痞子 提交于 2019-12-07 16:18:40

问题


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 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.



来源:https://stackoverflow.com/questions/48093609/java-9-how-to-add-further-jar-files-to-classpath-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!