使用URLClassLoader动态加载JAR?

我有一个程序需要能够在运行时动态加载JAR – 在环顾四周后我相信它使用URLClassLoader,但我不知道如何让它工作。 JAR“openup.jar”与程序位于同一目录中。

理想情况下,我希望能够加载此JAR,而无需在其中指定每个单独的类。

我成功使用了什么:

@SuppressWarnings("unchecked") public void addURL(URL u) throws IOException { URLClassLoader sysLoader = (URLClassLoader) ThisClass.class.getClassLoader(); URL urls[] = sysLoader.getURLs(); for (int i = 0; i < urls.length; i++) { if (urls[i].toString().equalsIgnoreCase(u.toString())) { return; } } Class sysclass = URLClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysLoader, new Object[] { u }); } catch (Throwable t) { throw new IOException("Error, could not add URL to system classloader"); } } 

在运行时如何动态加载Jars中确实提供了几乎相同的解决方案?