在我的项目类路径poi 2.5版本已经存在并且在运行时我想加载poi 3.5 jar,因为我的项目需要poi 3.5版本的两个java类,所以为此我编写了将调用poi的类加载器运行时3.5 jar请告诉我现在的错误,因为它没有在运行时加载poi 3.5版本的jar
下面的是我的类加载器
public class MyClassLoader {
private static final Class[] parameters = new Class[] {URL.class};
public static void addFile(String s) throws IOException
{
File f = new File(s);
addFile(f);
}
public static void addFile(File f) throws IOException
{
addURL(f.toURI().toURL());
}
public static void addURL(URL u) throws IOException
{
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] {u});
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}
}
}
这里我从main方法中调用poi 3.5类的方法之一,它在3.5中新添加但是没有加载
MyClassLoader.addFile("C:\\Release14branchupdated\\lib\\thirdparty\\POI-3.5\\poi-3.10-FINAL.jar");
Constructor<?> cs = ClassLoader.getSystemClassLoader().loadClass("org.apache.poi.ss.formula.FormulaCellCacheEntrySet").getConstructor(String.class);
System.out.println("$$$$$$$$$$"+cs.getName());
org.apache.poi.ss.formula.FormulaCellCacheEntrySet instance = (org.apache.poi.ss.formula.FormulaCellCacheEntrySet)cs.newInstance();
instance.iterator();
现在执行时我得到的错误是找不到类FormulaCellCacheEntrySet