我从外部.jar文件获取课程时遇到问题。我发现this加载一个外部.jar的方法,但是每当我运行它,我得到一个ClassNotFoundException。以下是我正在使用的代码,here是我正在测试的.jar文件。如果任何人有任何关于如何解决这个问题的想法,请告诉我。谢谢!Java:从外部.jar获取课程
这里是我的代码:
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
public void loadJar() throws MalformedURLException {
URL[] classes = {new URL("jar:file://test.jar!/")};
URLClassLoader child = new URLClassLoader (classes, this.getClass().getClassLoader());
try {
Class classToLoad = Class.forName ("test.PackageTest", true, child);
Method method = classToLoad.getDeclaredMethod ("test");
Object instance = classToLoad.newInstance();
Object result = method.invoke(instance);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
这是怎么回事? http://stackoverflow.com/questions/9880385/java-build-path – 2012-03-27 00:40:42