I am trying to make a plugin storage where my java code downloads the existing jars in the storage dynamically at run time . I implemented an interface which should be implemented by each plugin. Here is the code that i used ,
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();
}
and here is the Manifest of the Video plugin
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
When i tried to run this code it gave me this exception :
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.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.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$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.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 : This code works on other jars even with external libraries very well and this jar works well from the terminal .
org.apache.http.protocol.HttpCoreContext is not in httpcore 4.3.3 jar.
May be you are using wrong version of Jar or your approach is wrong.
当我查看此插件使用的库时,问题得到解决,我发现我正在使用类似的库,但版本较旧,因此我从类路径中删除了库,程序正常运行
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.