本文介紹了如何在JDK11+中動(dòng)態(tài)加載運(yùn)行時(shí)的JAR文件?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我有一個(gè)應(yīng)用程序,它使用以下解決方案在運(yùn)行時(shí)動(dòng)態(tài)加載JAR文件:
File file = ...
URL url = file.toURI().toURL();
URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, url);
這是使用How to load JAR files dynamically at Runtime?
的答案完成的
我現(xiàn)在想要一個(gè)適用于JDK11+的解決方案,與我使用的原始解決方案等價(jià)。因此,無(wú)需第三方庫(kù)/框架或加載/調(diào)用單個(gè)類(lèi)即可編程完成。
我嘗試了以下操作:
-
創(chuàng)建了擴(kuò)展UrlClassLoader的DynamicClassLoader:
public final class DynamicClassLoader extends URLClassLoader {
public DynamicClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
public DynamicClassLoader(String name, ClassLoader parent) {
super(name, new URL[0], parent);
}
public DynamicClassLoader(ClassLoader parent) {
this("classpath", parent);
}
public void addURL(URL url) {
super.addURL(url);
}
}
-
然后我使用java.system.class.loader標(biāo)志啟動(dòng)我的應(yīng)用程序:
java -Djava.system.class.loader=com.example.DynamicClassLoader
然后我有一個(gè)JAR文件作為路徑對(duì)象,我使用以下方法調(diào)用它:
public void loadJar(Path path) throws Exception {
URL url = path.toUri().toURL();
DynamicClassLoader classLoader = (DynamicClassLoader)ClassLoader.getSystemClassLoader();
Method method = DynamicClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, url);
}
調(diào)用此方法時(shí),我得到以下強(qiáng)制轉(zhuǎn)換類(lèi)異常:
class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class com.example.classloader.DynamicClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader is
in module java.base of loader 'bootstrap'; com.example.classloader.DynamicClassLoader is in unnamed module of loader 'app')
我在OpenJDK11(內(nèi)部版本號(hào)11.0.10+9)上使用Spring Boot(2.4)。
推薦答案
根據(jù)評(píng)論區(qū)中的討論,我找到了解決方案。這可能不像我希望的那樣通用,并假設(shè)您知道要使用的類(lèi)(相反,只需將Maven依賴(lài)項(xiàng)添加到pom.xml或舊的JDK8解決方案中)。
-
下載Maven依賴(lài)項(xiàng)(使用Jeka)
List<Path> paths = resolveDependency(groupId, artifactId, version);
public List<Path> resolveDependency(String groupId, String artifactId, String version) throws Exception {
String dependency = groupId + ":" + artifactId + ":" + version;
JkDependencySet deps = JkDependencySet.of()
.and(dependency)
.withDefaultScopes(COMPILE_AND_RUNTIME);
JkDependencyResolver resolver = JkDependencyResolver.of(JkRepo.ofMavenCentral());
List<Path> paths = resolver.resolve(deps, RUNTIME).getFiles().getEntries();
return paths;
}
-
加載JAR文件
List<Class> classes = loadDependency(paths);
public List<Class> loadDependency(List<Path> paths) throws Exception {
List<Class> classes = new ArrayList<>();
for(Path path: paths){
URL url = path.toUri().toURL();
URLClassLoader child = new URLClassLoader(new URL[] {url}, this.getClass().getClassLoader());
ArrayList<String> classNames = getClassNamesFromJar(path.toString());
for (String className : classNames) {
Class classToLoad = Class.forName(className, true, child);
classes.add(classToLoad);
}
}
return classes;
}
// Returns an arraylist of class names in a JarInputStream
private ArrayList<String> getClassNamesFromJar(JarInputStream jarFile) throws Exception {
ArrayList<String> classNames = new ArrayList<>();
try {
//JarInputStream jarFile = new JarInputStream(jarFileStream);
JarEntry jar;
//Iterate through the contents of the jar file
while (true) {
jar = jarFile.getNextJarEntry();
if (jar == null) {
break;
}
//Pick file that has the extension of .class
if ((jar.getName().endsWith(".class"))) {
String className = jar.getName().replaceAll("/", "\.");
String myClass = className.substring(0, className.lastIndexOf('.'));
classNames.add(myClass);
}
}
} catch (Exception e) {
throw new Exception("Error while getting class names from jar", e);
}
return classNames;
}
// Returns an arraylist of class names in a JarInputStream
// Calls the above function by converting the jar path to a stream
private ArrayList<String> getClassNamesFromJar(String jarPath) throws Exception {
return getClassNamesFromJar(new JarInputStream(new FileInputStream(jarPath)));
}
-
使用類(lèi)
就像雷納托指出的那樣,要知道您需要了解使用它的類(lèi)。在我的例子中,它是一個(gè)駱駝組件,我需要強(qiáng)制轉(zhuǎn)換并添加到這個(gè)框架中。類(lèi)是您在第二步中檢索到的類(lèi),方案是component的名稱(chēng)。
Component camelComponent = getComponent(classes, scheme);
context.addComponent(scheme, camelComponent);
public Component getComponent(List<Class> classes, String scheme) throws Exception {
Component component = null;
for(Class classToLoad: classes){
String className = classToLoad.getName().toLowerCase();
if(className.endsWith(scheme + "component")){
Object object = classToLoad.newInstance();
component = (Component) object;
}
}
return component;
}
因此,第二部分是使其動(dòng)態(tài)可用。添加了第一部分和第三部分,以獲得完整解決方案的示例。
這篇關(guān)于如何在JDK11+中動(dòng)態(tài)加載運(yùn)行時(shí)的JAR文件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,