Java 如何解决 ClassNotFoundException: com.mongodb.connection.BufferProvider?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29729331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?
提问by Chintan Patel
I am trying to write simple java code which shows the MongoDB collections on console. I have added mongodb-driver-3.0.0.jar in my classpath.
我正在尝试编写简单的 java 代码,它在控制台上显示 MongoDB 集合。我在我的类路径中添加了 mongodb-driver-3.0.0.jar。
But when I try to execute the code, it is giving me following error at the line of Database connection:
但是当我尝试执行代码时,它在数据库连接行出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/connection/BufferProvider at com.chintan.app.MongoDbJdbc.main(MongoDbJdbc.java:12) Caused by: java.lang.ClassNotFoundException: com.mongodb.connection.BufferProvider at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more
线程“main”中的异常 java.lang.NoClassDefFoundError: com/mongodb/connection/BufferProvider at com.chintan.app.MongoDbJdbc.main(MongoDbJdbc.java:12) 由:java.lang.ClassNotFoundException: com.mongodb.connection .BufferProvider at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more
Following is my code snippet:
以下是我的代码片段:
public static void main(String[] args)
{
MongoClient mongoClient = new MongoClient("localhost", 27017); //Exception
@SuppressWarnings("deprecation")
DB db = mongoClient.getDB("mydb");
System.out.println("Database connection successfull.");
...
...
}
What is the problem here? Do I need to add another jar in classpath or there is some version problem?
这里有什么问题?我是否需要在类路径中添加另一个 jar 或者存在一些版本问题?
采纳答案by bpulito
On the mongo driver page here: http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/installation-guide/#mongodb-driver
在此处的 mongo 驱动程序页面上:http: //mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/installation-guide/#mongodb-driver
You'll see the following text:
您将看到以下文本:
Note: mongodb-driver requires the following dependencies: bson and mongodb-driver-core
注意:mongodb-driver 需要以下依赖:bson 和 mongodb-driver-core
So you need all of the following jars to make this work:
所以你需要以下所有的 jars 来完成这项工作:
mongodb-driver-3.0.1.jar, mongodb-driver-core-3.0.1.jar, bson-3.0.1.jar
mongodb-driver-3.0.1.jar, mongodb-driver-core-3.0.1.jar, bson-3.0.1.jar
Which can be downloaded from here: https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.0.1/https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver-core/3.0.1/https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.1/
可以从这里下载:https: //oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.0.1/ https://oss.sonatype.org/content/repositories/releases /org/mongodb/mongodb-driver-core/3.0.1/ https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.1/
回答by Giancarlo Romeo
The following java driver contains the BufferProvider class:
以下 java 驱动程序包含 BufferProvider 类:
http://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.0.0
http://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.0.0
回答by Sylvain Bugat
This class is in the mongodb-driver-core-3.0.0
jar file which is required by mongodb-driver-3.0.0.jar
. You can see java drivers dependencies in the POM fileassociated with this jar.
I think you have to use a dependency manager to automatically add MongoDB (and other components) transitive dependencies to your project (maven, gradle, ...).
此类位于mongodb-driver-core-3.0.0
.jar 文件所需的jar 文件中mongodb-driver-3.0.0.jar
。您可以在与此 jar 关联的POM 文件中查看 java 驱动程序依赖项。
我认为您必须使用依赖项管理器自动将 MongoDB(和其他组件)传递依赖项添加到您的项目(maven、gradle 等)中。
回答by John Ajith A
If you don't have maven project, just include mongo-java-driver-3.6.1.jar (this jar is only for Java) in your project no need to include other jars.
如果您没有 maven 项目,只需在您的项目中包含 mongo-java-driver-3.6.1.jar(此 jar 仅适用于 Java),无需包含其他 jar。
回答by kamal kannan
include mongo-java-driver-3.11.0.jar file in class path not in Module path.
在类路径中而不是在模块路径中包含 mongo-java-driver-3.11.0.jar 文件。