跑java程序时出现找不到方法的错误

Exception in thread "pool-1-thread-2" java.lang.NoSuchMethodError: 'void com.mongodb.MongoNamespace.checkDatabaseNameValidity(java.lang.String)'
        at com.mongodb.ConnectionString.<init>(ConnectionString.java:370)
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:253)
        at com.mongo.config.MongoOneConfig.statisFactory(MongoOneConfig.java:65)
        at com.mongo.config.MongoOneConfig.oneMongoTemplate(MongoOneConfig.java:49)
        at com.mongo.config.MongoOneConfig.oneInstance(MongoOneConfig.java:32)
        at com.mongo.control.FileMongoBTest$1.run(FileMongoBTest.java:46)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at java.base/java.lang.Thread.run(Thread.java:831)
Exception in thread "pool-1-thread-1" java.lang.NoSuchMethodError: 'void com.mongodb.MongoNamespace.checkDatabaseNameValidity(java.lang.String)'
        at com.mongodb.ConnectionString.<init>(ConnectionString.java:370)
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:253)
        at com.mongo.config.MongoOneConfig.statisFactory(MongoOneConfig.java:65)
        at com.mongo.config.MongoOneConfig.oneMongoTemplate(MongoOneConfig.java:49)
        at com.mongo.config.MongoOneConfig.oneInstance(MongoOneConfig.java:32)
        at com.mongo.control.FileMongoBTest$1.run(FileMongoBTest.java:46)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at java.base/java.lang.Thread.run(Thread.java:831)

主要问题出现在包引用上面
看我的pom文件

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.11.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

引入了两个mongodb的包,导致在程序加载的时候不知道去哪个包里找,所以出现此报错,删掉一个引用包即可
只留下一个

      <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>

到此问题解决

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐