即使在类路径中的Jar中也找不到类 [英] Class not found even when in Jar in the classpath
问题描述
我正在尝试部署使用Jackson,JUnit和Commons-IO的应用程序。我在我的应用程序的类路径中有以下Jars:
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
此应用程序在我的开发中有效环境,我已经使用主应用程序jar部署了所有上述Jars。我可以毫无问题地运行应用程序,但每次我尝试使用它时都会出现以下故障:
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
ObjectMapper类位于jackson-databind-2.7.0.jar文件中。此外,我在访问其他jar文件(包括JUnit jars!)中的类时没有问题。出于某种原因,就好像类加载器正在加载每个Jar,除了jackson-databind-2.7.0.jar。
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.
事实证明,当您将代码导出到JAR时,我正在使用的环境(Eclipse!)无法正确更新清单文件文件。如果没有正确的清单条目,应用程序就无法找到依赖的jar文件。
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.
在我看来,这是对Eclipse人员的严重疏忽 - - 特别是当你的应用程序依赖于很多jar文件时。为了让我的应用程序运行,我有以下选择:
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:
- 创建一个运行jvm的脚本,并有一个参数列表到每个需要的jar文件,或者:
2:手动将每个必需的jar文件输入清单文件
2: Manually enter each required jar file into the Manifest file
据我所知,没有办法自动更新清单文件。这是一个严肃的PITA(A **中的痛苦)......
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...
这篇关于即使在类路径中的Jar中也找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!