由于:java.lang.ClassNotFoundException: 找不到 com.fasterxml.jackson.databind.JavaType

7

我在我的应用程序中使用jackson库,当我构建代码(使用ant)时,构建成功。我已经尝试过在单元测试中使用这些方法进行模拟测试,并且运行良好。但是当启动karaf时,它会给我这个错误:

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JavaType not found by com.project.test.application [224]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

这些是我在ivy.xml中下载的jar包:

<dependency org="com.fasterxml.jackson.core" name="jackson-core" rev="2.4.4" transitive="false" />
<dependency org="com.fasterxml.jackson.core" name="jackson-annotations" rev="2.4.4" transitive="false" />
<dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.4.4" transitive="false" />

我尝试更改版本,但仍然出现相同的错误。我需要在下载后加载这些jars吗?我已经将默认位置设置为ivy-lib,并且该过程对我下载的所有其他jars都有效。
出了什么问题?这是否需要特定版本或任何其他依赖项?或者有其他方法可以加载这些jar文件?
build.xml中我的类路径是什么?
<path id="compile.classpath">
    <fileset dir="${ivy.lib}">
      <include name="*.jar"/>
    </fileset>
</path>

你的com.test.application类的代码在哪里? - developer
当运行测试时,您如何指定类路径?您需要从build.xml文件中包含更多细节。该类存在于jackson-databind依赖项中:http://search.maven.org/#search%7Cga%7C1%7Cfc%3A%22com.fasterxml.jackson.databind.JavaType%22%20AND%20g%3A%22com.fasterxml.jackson.core%22%20AND%20v%3A%222.4.4%22 - Mark O'Connor
我的 build.xml 中的类路径是 ivy-lib 文件夹。我已经更新了我的答案。 - Siddharth
3个回答

11

我之所以遇到这个错误,是因为其他班级的某个人在使用org.codehaus.jackson包,而我正在使用com.fasterxml.jackson包。由于jar包的冲突,它会抛出这个ClassNotFoundException异常。一旦我删除并替换那些fasterxml的jar包为codehaus的jar包,它就开始正常工作了。


4
我们可以在我们的pom.xml文件中添加以下依赖项,这样所有类型的jackson错误都将被解决。
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.4</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.4</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.4</version>
    </dependency>

2
我不明白- 为什么你要明确排除核心和注释,然后再添加它们? - IcedDante

0
对我来说,版本2.8.6在没有Spring Boot的情况下无法与Spring MVC上下文一起使用,而2.9.4可以正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接