java - How to get resource files when loading a jar using an URLClassLoader? -


i have written application manages several plugins provided jars. load plugin classes using urlclassloader works supposed.

but writing plugin loads resources stored inside jar. if start plugin standalone application works, if start inside application nullpointerexception when try open resources inputstream.

i open stream this:

this.getclass().getresourceasstream("/templates/template.html"); 

my eclipse project structure looks like:

src | + source files resources | + templates   |   + template.html 

the following loads plugins:

private list<class<?>> loadclasses(final file[] jars) {     list<class<?>> classes = new arraylist<class<?>>();     url[] urls = getjarurls(jars);     urlclassloader loader = new urlclassloader(urls);      (file jar : jars) {         jarfile jarfile = null;          try {             jarfile = new jarfile(jar);         } catch (ioexception e) {             // skip jar if can not opened             continue;         }          enumeration<jarentry> entries = jarfile.entries();          while (entries.hasmoreelements()) {             jarentry entry = entries.nextelement();              if (isclassfile(entry.getname())) {                 string classname = entry.getname().replace("/", ".").replace(".class", "");                 class<?> cls = null;                  try {                     cls = loader.loadclass(classname);                 } catch (classnotfoundexception e) {                     // skip jar if class inside can not loaded                     continue;                 }                  classes.add(cls);             }         }          try {             jarfile.close();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     }      try {         loader.close();     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     }      return classes; }  /**  * checks if path points class file or not.  *   * @param path filename check  * @return {@code true} if path points class file or {@code false}  *         if not  */ private boolean isclassfile(final string path) {     return path.tolowercase().endswith(".class") && !path.tolowercase().contains("package-info"); } 

then make instances classes using newinstance().

i think root path of plugins jar not same root path of application or not contents of jar files loaded or both...

can me?

first note, using getclass().getresource(...) delegates classloader, responsible loading resources. class loader used? same class loader, class loaded. point.

in code, build urlclassloader loading classes. same urlclassloader used loading resources, if above mentioned call comes class inside plugin.

this seems ok ... did little mistake. @ end of loading closed loader. prevent subsequent calls loadclass or getresource returning meaningful. in fact, null, loader cannot load resource anymore.

conclusion: not close urlclassloader, if still need ofr loading purposes. instead keep reference class loader , close @ end of program runtime.


Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -