我们有一个可执行的JAR文件,有时包含其他JAR文件。(整个过程都取决于其他四个下载的罐子,它们骑在一只巨大的部署在太空中的海龟的背上。)在运行时,我们动态加载嵌套的JAR文件,执行以下操作:
// wearyingly verbose error handling elided
URL nestedURL = the_main_system_classloader.getResource("path/to/nested.jar");
File temp = File.createTempFile (....);
// copy out nestedURL contents into temp, byte for byte
URL tempURL = temp.toURI().toURL();
URLClassLoader loader = URLClassLoader.newInstance(new URL[]{ tempURL });
Class<?> clazz = loader.loadClass("com.example.foo.bar.baz.Thing");
Thing thing = (Thing) clazz.newInstance();
// do stuff with thing这种技术以前在这里已经提过;链接包括这一个和这一个。我们现有的代码有效..。
...mostly。我真的很想找到一些方法来避免临时文件的创建和复制(以及最终的清理,因为我们都知道,deleteOnExit是邪恶的)。毕竟,在起始点获得的URL指向一个JAR:
URL nestedURL = the_main_system_classloader.getResource("path/to/nested.jar");
// nestedURL.toString() at this point is
// "jar:file:/C:/full/path/to/the/executable.jar!/path/to/nested.jar"
URLClassLoader loader = URLClassLoader.newInstance(new URL[]{ nestedURL });
Class<?> clazz = loader.loadClass("com.example.foo.bar.baz.Thing");但是loadClass抛出一个ClassNotFound。
URLClassLoader可以简单地不处理这个JAR-的情况吗?或者,我是否需要对所涉及的一个或多个路径( nestedURL或传递给loadClass的字符串)做些什么才能做到这一点?
发布于 2014-06-17 21:22:22
香草URLClassloader不能处理它。
到目前为止这是你的答案。
除了您的解决方案之外,我还知道另外两种可能性:
https://stackoverflow.com/questions/19015447
复制相似问题