java - load resource from Jar file at runtime -


i trying load resource jar file added in @ runtime , not getting far.

here 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 error: java.util.zip.zipexception: error in opening zip file @ java.util.zip.zipfile.open(native method)

questions:

1) need put "test.resource" in url? 2) relationship between urlclassloader , current class's classloader? 3) proper way stream in resource (obviously have doesn't work)?

thanks

if want add jar file current thread's classloader you'd create new urlclassloader, use current class's classloader parent , add new jar urls. may not put test.resource in url. don't forget assign new classloader current thread. see 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 should able file via thread.currentthread().getcontextclassloader().getresourceasstream.

if want read file jar without adding classpath can take 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()) } 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -