java - Creating Jar and loading classes in it -


i need create jar , load classes inside jar using classloader. use following code create folder structure inside jar.this works fine when try load class jar throws exception classnotfound.

public void createjar() throws ioexception {     //fileoutputstream stream = new fileoutputstream("d:\\myfolder\\apache-tomcat-6.0.32\\webapps\\springtest\\web-inf\\lib\\servicejar.jar");     fileoutputstream stream = new fileoutputstream("d:/myfolder/apache-tomcat-6.0.32/webapps/springtest/web-inf/lib/servicejar.jar");     jaroutputstream target = new jaroutputstream(stream, new manifest());     add(new file("d:/myjar"), target);     target.close();     stream.close(); }  private void add(file source, jaroutputstream target) throws ioexception {    byte buffer[] = new byte[10204];   try   {     if (source.isdirectory())     {       string name = source.getpath().replace("\\", "/");       if (!name.isempty())       {         if (!name.endswith("/"))           name += "/";         jarentry entry = new jarentry(name);         entry.settime(source.lastmodified());         target.putnextentry(entry);         target.closeentry();       }       (file nestedfile: source.listfiles())         add(nestedfile, target);       return;     }      jarentry entry = new jarentry(source.getpath().replace("\\", "/"));    // jarentry entry = new jarentry(source.getpath());     entry.settime(source.lastmodified());     target.putnextentry(entry);        fileinputstream in = new fileinputstream(source);     while (true) {       int nread = in.read(buffer, 0, buffer.length);       if (nread <= 0)         break;       target.write(buffer, 0, nread);     }     in.close();     target.closeentry();   }   catch(exception e){    }     {    } } 

classes jared @ location "d:\myjar\spring\controller\mycontroller.class" , " d:\myjar\spring\service\myservice.class".so jar created want , contains folder structure(mycontroller.class inside jar @ location \myjar\spring\controller).but when try load following code exception

public void loadjar() {     //file root = new file("d:\\myfolder\\apache-tomcat-6.0.32\\webapps\\springtest\\web-inf\\lib\\servicejar.jar");//     file root = new file("d:/myfolder/apache-tomcat-6.0.32/webapps/springtest/web-inf/lib/servicejar.jar");//     urlclassloader classloader;     try {              classloader = urlclassloader.newinstance(new url[] { root.touri().tourl() });              class<?> cls = class.forname("myjar.spring.controller.mycontroller", true, classloader);             object instance = cls.newinstance(); // should print constructor content             system.out.println(instance);             method m =cls.getmethod("printthis", null);             m.invoke(instance, null);                        } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     }   }  

but if create jar without package structure inside it.it works fine. have checked paths , correct.also location of class file inside jar correct. please explain why getting classnotfound exception .also suggest if change required in createjar() or add() code.

you may need add class-path on go, following link of help.

how change classpath within java?


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -