在运行时从Jar文件加载资源 [英] load resource from Jar file at runtime

查看:110
本文介绍了在运行时从Jar文件加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从运行时添加的Jar文件中加载资源,但运行得并不快。



这是我的代码(groovy):

  URL url = new URL(jar:file:/out/resource.jar!/test.resource)
def urlList = []<< url
URL [] urls = urlList.toArray()
URLClassLoader classLoader = new URLClassLoader(urls,this.class.getClassLoader())
InputStream stream = url.openStream()

我得到这个错误:
java.util.zip.ZipException:打开zip文件$ b时出错$ b在java.util.zip.ZipFile.open(本地方法)

问题:

1)我需要把test.resource放入我的网址吗?
2)我的URLClassLoader和我的当前类的类加载器之间有什么关系?
3)什么是正确的方式来流这个资源(显然我有什么不工作)?

谢谢 $ b $如果你想添加一个JAR文件到你当前线程的ClassLoader中,你需要创建一个新的URLClassLoader,使用当前类的ClassLoader作为父类,并添加新的JAR网址。您不得在您的网址中放置 test.resource 。不要忘记将新的ClassLoader分配给当前线程。请参阅下面的示例:

pre $ URL url = new URL(file:/out/resource.jar)
def urlList = []<< url
URL [] urls = urlList.toArray()
URLClassLoader classLoader = new URLClassLoader(urls,getClass()。classLoader)
Thread.currentThread()。setContextClassLoader(classLoader)

现在您应该可以通过 Thread.currentThread()。getContextClassLoader ).getResourceAsStream



如果实际上只想从JAR中读取文件而不将其添加到类路径中,则可以采用以下方法:

  JarFile jar = new JarFile(new File('/ out / resource.jar'))
JarEntry jarEntry = jar.getJarEntry('test.resource')

if(jarEntry){
File file = new File(new URL(jar:file:/out/resource.jar!/ test.resource).toURI())
}


I am trying to load a resource from a Jar file added in at runtime and not getting very far.

Here is my code (groovy):

URL url = new URL("jar:file:/out/resource.jar!/test.resource")
def urlList = [] << url
URL[] urls = urlList.toArray()
URLClassLoader classLoader = new URLClassLoader(urls, this.class.getClassLoader())
InputStream stream = url.openStream()

I get this error: java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method)

Questions:

1) do I need to put "test.resource" in my url? 2) What is the relationship between my URLClassLoader and my current class's classloader? 3) what is the proper way to stream in this resource (obviously what I have doesn't work)?

Thanks

解决方案

If you want to add a JAR file to your current thread's ClassLoader you'd create a new URLClassLoader, use the current class's ClassLoader as parent and add the new JAR URLs. You may not put test.resource in your URL. Don't forget to assign the new ClassLoader to your current thread. See the example below:

URL url = new URL("file:/out/resource.jar")
def urlList = [] << url
URL[] urls = urlList.toArray()
URLClassLoader classLoader = new URLClassLoader(urls, getClass().classLoader)
Thread.currentThread().setContextClassLoader(classLoader)

Now you should be able to get the file via Thread.currentThread().getContextClassLoader().getResourceAsStream.

If you actually just want to read the file from a JAR without adding it to your classpath you can take the following approach:

JarFile jar = new JarFile(new File('/out/resource.jar'))
JarEntry jarEntry = jar.getJarEntry('test.resource')

if(jarEntry) {
    File file = new File(new URL("jar:file:/out/resource.jar!/test.resource").toURI())
}

这篇关于在运行时从Jar文件加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆