我從外部.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