我不知道为什么会出现这个错误.我试着清理多余的lib等等.

当我try 执行以下操作时,会出现错误:

  mongo = new Mongo(host, port);

有趣的是,我可以运行从IDE(IntelliJ)生成的任何单元测试,但当我try 通过Tomcat运行它时,我得到了以下结果:

  Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.
    sfatandrei.db.MongoManager]: Constructor threw exception; nested exception is java.lang.IncompatibleCla
    ssChangeError: Implementing class
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInst
    antiationStrategy.java:110)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(Constructo
    rResolver.java:280)
        ... 82 more
    Caused by: java.lang.IncompatibleClassChangeError: Implementing class
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2889)
        at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1170)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2889)
        at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1170)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
        at com.mongodb.MongoOptions.reset(MongoOptions.java:47)
        at com.mongodb.MongoOptions.<init>(MongoOptions.java:29)
        at com.mongodb.Mongo.<init>(Mongo.java:148)
        at com.mongodb.Mongo.<init>(Mongo.java:137)
        at com.sfatandrei.db.MongoManager.<init>(MongoManager.java:28)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.
    java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
        ... 84 more

mvn dependency:tree -Dverbose

[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ mongodb-performance-test ---
[INFO] com.sfatandrei:mongodb-performance-test:war:1.0
[INFO] +- junit:junit:jar:4.8.2:test
[INFO] +- org.springframework:spring-core:jar:3.1.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-asm:jar:3.1.1.RELEASE:compile
[INFO] |  \- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-beans:jar:3.1.1.RELEASE:compile
[INFO] |  \- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-context:jar:3.1.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:3.1.1.RELEASE:compile
[INFO] |  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-asm:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-expression:jar:3.1.1.RELEASE:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  \- (org.springframework:spring-asm:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-jdbc:jar:3.1.1.RELEASE:compile
[INFO] |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.springframework:spring-tx:jar:3.1.1.RELEASE:compile
[INFO] |     +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-aop:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-context:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-web:jar:3.1.1.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  \- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-webmvc:jar:3.1.1.RELEASE:compile
[INFO] |  +- (org.springframework:spring-asm:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-context-support:jar:3.1.1.RELEASE:compile
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-context:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-expression:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  \- (org.springframework:spring-web:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework.data:spring-data-mongodb:jar:1.0.0.RELEASE:compile
[INFO] |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-expression:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework.data:spring-data-commons-core:jar:1.2.0.RELEASE:compile
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-tx:jar:3.1.1.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.mongodb:mongo-java-driver:jar:2.7.1:compile
[INFO] +- log4j:log4j:jar:1.2.16:compile
[INFO] +- org.springframework:spring-test:jar:3.1.1.RELEASE:compile
[INFO] +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- org.mongodb:bson:jar:2.3:compile
[INFO] \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.6:compile
[INFO]    \- org.codehaus.jackson:jackson-core-asl:jar:1.9.6:compile

我使用的是jdk 7、tomcat 7.0.27和maven 3.0.3.

推荐答案

这看起来像是字节码级别的错误.也许tomcat服务器中有两个版本的同一个库.

见:What causes java.lang.IncompatibleClassChangeError?

Mongodb相关问答推荐

如何使用MongoDB对子文档进行条件投影?

为什么我的Mongoose更新找不到匹配?

根据参考图计算子文档列表的最大值

MongoDB 投影按数组中的字符串长度排序

在数组对象 Mongodb 中仅 Select 需要的数组

如何获取键值对的对象,其中值仅具有 mongoDB 中的投影字段

MongoDB 根据最新日期查询多个不同的值

尽管前一阶段输出文档,$group stage 仍返回零文档

Mongo 聚合将 $sort 与 $geoNear 结合使用

MongoDB 将 JSON 字符串转换为数组[{obj1},{obj2}]的实际对象

当属性确实存在时,为什么mongoose模型的 hasOwnProperty 返回 false?

使用 brew upgrade Mongo update from 3.4 to 4.0 报错:在try 升级到 4.0 之前,数据文件需要完全升级到 3.6 版

python mongodb正则表达式:忽略大小写

Golang + MongoDB 嵌入类型(将一个 struct 嵌入到另一个 struct 中)

MongoDB:查询和检索嵌入式数组中的对象?

在 MongoDB 中快速搜索数十亿个小文档的策略

将新值推送到 mongodb 内部数组 - mongodb/php

验证 MongoCredential 的异常和未分类的 Mongo Db 异常

查找聚合性能差

哪个数据库适合我的应用程序 mysql 或 mongodb ?使用 Node.js 、 Backbone 、 Now.js