本文介紹了如何在JDK11+中動態(tài)加載運行時的JAR文件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個應用程序,它使用以下解決方案在運行時動態(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)在想要一個適用于JDK11+的解決方案,與我使用的原始解決方案等價。因此,無需第三方庫/框架或加載/調用單個類即可編程完成。
我嘗試了以下操作:
-
創(chuàng)建了擴展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標志啟動我的應用程序:
java -Djava.system.class.loader=com.example.DynamicClassLoader
然后我有一個JAR文件作為路徑對象,我使用以下方法調用它:
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);
}
調用此方法時,我得到以下強制轉換類異常:
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(內部版本號11.0.10+9)上使用Spring Boot(2.4)。
推薦答案
根據(jù)評論區(qū)中的討論,我找到了解決方案。這可能不像我希望的那樣通用,并假設您知道要使用的類(相反,只需將Maven依賴項添加到pom.xml或舊的JDK8解決方案中)。
-
下載Maven依賴項(使用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)));
}
-
使用類
就像雷納托指出的那樣,要知道您需要了解使用它的類。在我的例子中,它是一個駱駝組件,我需要強制轉換并添加到這個框架中。類是您在第二步中檢索到的類,方案是component的名稱。
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;
}
因此,第二部分是使其動態(tài)可用。添加了第一部分和第三部分,以獲得完整解決方案的示例。
這篇關于如何在JDK11+中動態(tài)加載運行時的JAR文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,