ClassLoad失败。 ClassNotFound异常

时间:2017-10-07 01:32:29

标签: java jar classloader java-7 conflicting-libraries

我正在尝试从JAR文件加载一个类。该类称为BasicLineFormatter。因为我使用java -cp命令使用shell脚本来声明该类。 BasicLineFormatter类存在于我的两个jar中,一个带有com.ibm.jaxws.thinclient_8.5.0.jar,另一个是第三方jar。但我试图从其中一个罐子(第三方罐子)加载它。所以我添加了这段代码:

JarFile jarFile = new JarFile("Path to JAR");
Enumeration<JarEntry> enumeration = jarFile.entries();

URL[] urls = { new URL("jar:file:" + "Path to JAR" + "!/") };
URLClassLoader cl = URLClassLoader.newInstance(urls);

System.out.println("Stating to load");
while (enumeration.hasMoreElements()) {
    JarEntry je = enumeration.nextElement();
    if(je.isDirectory() || !je.getName().endsWith(".class")){
        continue;
    }
    String className = je.getName().substring(0,je.getName().length()-6);
    if (className.contains("BasicLineFormatter")) {
        System.out.println("Class Name: " + className);
        className = className.replace('/', '.');
        Class c = cl.loadClass(className);
    }

}

但在那之后我仍然得到错误:

Exception in thread "main" java.lang.NoSuchFieldError: org/apache/http/message/BasicLineFormatter.INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:236)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:82)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:95)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:104)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:62)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:236)

你能告诉我这里发生了什么,或者我做错了什么。

0 个答案:

没有答案