lundi 7 mars 2016

loading a java class at runtime through java

I have a jar file which is separate folder named lib so i am trying to load a jar from there as show in the below code but i am getting exception that is malformed exception please advise how can i correct this please advise

import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;

public class JarFileLoader extends URLClassLoader {
  public JarFileLoader(URL[] urls) {
    super(urls);
  }

  public void addFile(String path) throws MalformedURLException {
    String urlPath = "jar:file://" + path + "!/";
    addURL(new URL(urlPath));
  }

  public static void main(String args[]) {
    try {
      System.out.println("First attempt...");
      Class.forName("org.apache.poi.ss.usermodel.HssfSheet");
    } catch (Exception ex) {
      System.out.println("Failed.");
    }
    try {
      URL urls[] = {};
      JarFileLoader cl = new JarFileLoader(urls);
      cl.addFile("/lib/poi3.10final.jar");
      System.out.println("Second attempt...");
      cl.loadClass("org.apache.poi.ss.usermodel.HssfSheet");
      System.out.println("Success!");
    } catch (Exception ex) {
      System.out.println("Failed.");
      ex.printStackTrace();
    }
  }
}





Aucun commentaire:

Enregistrer un commentaire