简体   繁体   中英

Class not found even when in Jar in the classpath

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...

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...

Exactly same exception occurred in my project also but I am using sts. But exception showing main spring boot application class

(SpringApplication.java: 227) at org.springframework.boot.SpringApplication. (SpringApplication.java: 240) at org.springframework.boot.SpringApplication.run (SpringApplication.java:1301)

ApplicationStartup at java.base\/jdk.internal.loader.BuiltinClassLoader.loadClass (BuiltinClassLoader.java:606)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM