Dynamic loading of java class for activiti Service Task -
i need dynamically load jar , extract classes classpath, there no need of restarting server. activi service task, java class available jar. tried out below, @ point service task deployed gives error "cannot instantiate packagename.classname. " guess means class cannot found.
urlclassloader loader = (urlclassloader)classloader.getsystemclassloader(); log.info("loader" +loader.geturls()); myclassloader l = new myclassloader(loader.geturls()); l.addurl(new url("jar","","file:"+artifactlocation+"!/")); jarfile jarfile = new jarfile(artifactlocation); enumeration e = jarfile.entries(); while (e.hasmoreelements()) { jarentry je = (jarentry) e.nextelement(); if (je.isdirectory() || !je.getname().endswith(".class")) { continue; } // -6 because of .class string classname = je.getname().substring(0, je.getname().length() - 6); classname = classname.replace('/', '.'); class c = l.loadclass(classname);
any idea on how solve custom classloader other osgi method?
- you cannot safely assume system classloader instance of
urlclassloader
. is, might not true next version of java. - no need write own classloader,
urlclassloader
fine. , when instantiatedurlclassloader(url[] urls)
constructor, delegate parent classloader, need url of own .jar file(s). - no need extract .class files,
classloader
knows how load classes .jar file - you might want set custom classloader current - along
thread.currentthread().setcontextclassloader(l)
- eventually, might load classes using
class.forname("my.foo.class")
Comments
Post a Comment