使用 URLClassLoader 时出现 NoClassDefFoundError

NoClassDefFoundError when using URLClassLoader

我正在尝试创建一个插件存储,我的 java 代码会在 运行 时间动态下载存储中现有的 jar。我实现了一个应该由每个插件实现的接口。 这是我使用的代码,

 public void importPlug() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    File jar = new File ("/home/karim/Downloads/yarab/VideoPlugin.jar");
    JarFile jf = new JarFile(jar);
     ArrayList <URL> urls = new ArrayList<URL>();
     urls.add(jar.toURI().toURL());
     java.util.jar.Manifest mf =  jf.getManifest();
       if (mf != null) {
           String cp =
                   ((java.util.jar.Manifest) mf).getMainAttributes().getValue("class-path");
           if (cp !=
                   null) {
               for (String cpe : cp.split("\s+")) {
                   File lib =
                           new File(jar.getParentFile(), cpe);
                   urls.add(lib.toURI().toURL());
               }
           }
       }
       for (URL u : urls) {
           System.out.println(u.toString());
       }
       ClassLoader cl =
               new URLClassLoader(urls.toArray(new URL[urls.size()]), PluginInterface.class.getClassLoader());
       //Class c = cl.loadClass("VideoPlugin");
     Class c = Class.forName("selenium.VideoPlugin", true, cl);
       pi = (PluginInterface) c.newInstance();

}

这是视频插件的清单

 Manifest-Version: 1.0
Class-Path: . VideoPlugin_lib/apache-mime4j-0.6.jar VideoPlugin_lib/bs
 h-1.3.0.jar VideoPlugin_lib/cglib-nodep-2.1_3.jar VideoPlugin_lib/com
 mons-codec-1.9.jar VideoPlugin_lib/commons-collections-3.2.1.jar Vide
 oPlugin_lib/commons-exec-1.1.jar VideoPlugin_lib/commons-io-2.4.jar V
 ideoPlugin_lib/commons-jxpath-1.3.jar VideoPlugin_lib/commons-lang3-3
 .3.2.jar VideoPlugin_lib/commons-logging-1.1.3.jar VideoPlugin_lib/cs
  sparser-0.9.14.jar VideoPlugin_lib/gson-2.3.1.jar VideoPlugin_lib/gua
  va-18.0.jar VideoPlugin_lib/hamcrest-core-1.3.jar VideoPlugin_lib/ham
   crest-library-1.3.jar VideoPlugin_lib/htmlunit-2.15.jar VideoPlugin_l
   ib/htmlunit-core-js-2.15.jar VideoPlugin_lib/httpclient-4.3.6.jar Vid
   eoPlugin_lib/httpcore-4.3.3.jar VideoPlugin_lib/httpmime-4.3.6.jar Vi
   deoPlugin_lib/ini4j-0.5.2.jar VideoPlugin_lib/jcommander-1.29.jar Vid
    eoPlugin_lib/jetty-websocket-8.1.8.jar VideoPlugin_lib/jna-3.4.0.jar 
    VideoPlugin_lib/jna-platform-3.4.0.jar VideoPlugin_lib/json-simple-1.
    1.1.jar VideoPlugin_lib/junit-dep-4.11.jar VideoPlugin_lib/nekohtml-1
   .9.21.jar VideoPlugin_lib/netty-3.5.7.Final.jar VideoPlugin_lib/opera
   driver-1.5.jar VideoPlugin_lib/phantomjsdriver-1.2.1.jar VideoPlugin_
   lib/protobuf-java-2.4.1.jar VideoPlugin_lib/sac-1.3.jar VideoPlugin_l
   ib/selenium-java-2.45.0-srcs.jar VideoPlugin_lib/selenium-java-2.45.0
   .jar VideoPlugin_lib/serializer-2.7.1.jar VideoPlugin_lib/testng-6.8.
    5.jar VideoPlugin_lib/xalan-2.7.1.jar VideoPlugin_lib/xercesImpl-2.11
    .0.jar VideoPlugin_lib/xml-apis-1.4.01.jar 

当我尝试 运行 这段代码时,它给了我这个异常:

 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/protocol/HttpCoreContext
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access0(URLClassLoader.java:71)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:412)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.gargoylesoftware.htmlunit.HttpWebConnection.<init>(HttpWebConnection.java:147)
at com.gargoylesoftware.htmlunit.WebClient.createWebConnection(WebClient.java:1907)
at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:134)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.newWebClient(HtmlUnitDriver.java:303)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:277)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:148)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:195)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:191)
at selenium.Search.search(Search.java:17)
at selenium.Search.sendLinks(Search.java:101)
at selenium.VideoPlugin.getSize(VideoPlugin.java:163)
at MobileCloud.Test.main(Test.java:56)
Caused by: java.lang.ClassNotFoundException: org.apache.http.protocol.HttpCoreContext
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 25 more

PS:即使使用外部库,此代码也能在其他 jar 上运行良好,并且此 jar 在终端上运行良好。

org.apache.http.protocol.HttpCoreContext 不在 httpcore 4.3.3 jar 中。

可能是您使用的 Jar 版本错误或者您的方法有误。

当我检查这个插件使用的库时问题就解决了,我发现我使用的是一个类似的库,但使用的是旧版本,所以我从类路径和程序中删除了我的库 运行 正确