Class not found even when in Jar in the classpath

那年仲夏 提交于 2019-12-12 02:59:31

问题


I am trying to deploy an application that is using Jackson, JUnit, and Commons-IO. I have the following Jars in my application's classpath:

commons-io-2.4.jar
jackson-databind-2.7.0.jar
jackson-annotations-2.7.0.jar
log4j-api-2.4.1.jar
wsdiscovery-0.2.jar
jackson-core-2.7.0.jar
log4j-core-2.4.1.jar

This application works within my development environment, and I have deployed all of the above Jars with the main application jar. I can run the application without problems, but every time I try to use it I get the following failure:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
    at com.oncam.hware.app.OnvifApp.formatOutput(OnvifApp.java:356)
    at com.oncam.hware.app.OnvifApp.dispatchCommand(OnvifApp.java:271)
    at com.oncam.hware.app.OnvifApp.loopSocket(OnvifApp.java:130)
    at com.oncam.hware.app.OnvifApp.useSocket(OnvifApp.java:216)
    at com.oncam.hware.app.OnvifApp.main(OnvifApp.java:473)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

The ObjectMapper class is in the jackson-databind-2.7.0.jar file. Furthermore, I have no problems accessing the classes in the other jar files (including the JUnit jars!). For some reason, it is as if the classloader is loading every Jar except jackson-databind-2.7.0.jar.

Does anyone know what is causing this and how I can fix it?

Someone please advise...


回答1:


I figured out what was wrong.

It turns out that the environment I am using (Eclipse!) does not properly update the manifest file when you export your code to a JAR file. Without the proper manifest entries, the application cannot "find" the dependent jar files.

This is, in my opinion, a serious oversight on the part of the Eclipse folks -- especially when you have an application that depends on a lot of jar files. In order to make my application run, I had the following choices:

  1. Create a script that runs the jvm and has a list of parameters pointing to every needed jar file, or:

2: Manually enter each required jar file into the Manifest file

To my knowledge, there is no way to automatically update the manifest file. This is a serious PITA (Pain In The A**)...

Anyway, sorry for bothering people about this problem. Hopefully, posting this answer will help others avoid similar problems...



来源:https://stackoverflow.com/questions/36044107/class-not-found-even-when-in-jar-in-the-classpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!