public LoadEngine() {
Lookup ocrengineLookup;
Collection<OCREngine> ocrengines;
Template ocrengineTemplate;
Result ocrengineResults;
try {
//ocrengineLookup = Lookup.getDefault(); this only load OCREngine in classpath of application
ocrengineLookup = Lookups.metaInfServices(getClassLoaderForExtraModule());//this load the OCREngine in the extra module as well
ocrengineTemplate = new Template(OCREngine.class);
ocrengineResults = ocrengineLookup.lookup(ocrengineTemplate);
ocrengines = ocrengineResults.allInstances();//all OCREngines must implement the defined interface in OCREngine. Reference to guideline of implement org.openide.util.Lookup for more information
} catch (Exception ex) {
}
}
public ClassLoader getClassLoaderForExtraModule() throws IOException {
List<URL> urls = new ArrayList<URL>(5);
//foreach( filepath: external file *.JAR) with each external file *.JAR, do as follows
File jar = new File(filepath);
JarFile jf = new JarFile(jar);
urls.add(jar.toURI().toURL());
Manifest mf = jf.getManifest(); // If the jar has a class-path in it's manifest add it's entries
if (mf
!= null) {
String cp =
mf.getMainAttributes().getValue("class-path");
if (cp
!= null) {
for (String cpe : cp.split("\\s+")) {
File lib =
new File(jar.getParentFile(), cpe);
urls.add(lib.toURI().toURL());
}
}
}
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (urls.size() > 0) {
cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), ClassLoader.getSystemClassLoader());
}
return cl;
}
5 回答
使用现有数据重新加载现有类可能会破坏事物 .
您可以相对轻松地将新代码加载到新的类加载器中:
不再使用的类加载器可以被垃圾收集(除非存在内存泄漏,通常使用ThreadLocal,JDBC驱动程序,
java.beans
等) .如果你想保留对象数据,那么我建议一个持久性机制,如序列化,或者你习惯的任何东西 .
当然,调试系统可以做更好的事情,但更麻烦,更不可靠 .
可以将新类添加到类加载器中 . 例如,使用
URLClassLoader.addURL
. 但是,如果一个类无法加载(因为,比如说,你没有添加它),那么它将永远不会加载到该类加载器实例中 .这对我有用:
您可能希望将系统基于OSGi(或者至少需要花费很多时间),这正是针对这种情况而制定的 .
与类加载器混淆是非常棘手的业务,主要是因为类可见性如何工作,并且您不希望以后遇到难以调试的问题 . 例如,在许多库中广泛使用的Class.forName()在碎片类加载器空间上不能很好地工作 .
我google了一下,发现这段代码here:
有人可以就这种方法分享意见/评论/答案吗?
使用org.openide.util.Lookup和ClassLoader动态加载Jar插件,如下所示 .