使用MongoDB驱动程序时java.lang.NoClassDefFoundError [英] java.lang.NoClassDefFoundError when using MongoDB driver

查看:4940
本文介绍了使用MongoDB驱动程序时java.lang.NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import org。

我正试图使用​​servlet上的Java驱动程序连接到在mlab上托管的MongoDB数据库。 bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoConnection {

protected void connectToMongo(String loc){

字符串dbName =读数;
String collection =data;

MongoClientURI uri = new MongoClientURI(mongodb:// user:pass@ds143109.mlab.com:43109 / reads);
MongoClient client = new MongoClient(uri);
MongoDatabase db = client.getDatabase(dbName);

MongoCollection< Document> reading = db.getCollection(collection);

文档doc = Document.parse(loc);

readings.insertOne(doc);

client.close();
}
}

问题是我收到以下错误:
java.lang.NoClassDefFoundError:com / mongodb / MongoClientURI



我看了一个答案(如何解决ClassNotFoundException:com.mongodb.connection.BufferProvider?)对我来说,我需要其他罐子,我已经下载了,但我仍然收到这个错误。



我正在使用Eclipse并将三个jar添加到构建路径,通过右键单击项目导航菜单,然后按照构建路径 - >配置构建路径 - > Java构建路径 - >图书馆 - >添加外部JAR



这是正确的方法吗?还有其他我应该做的事情吗?

解决方案

你有 java.lang .NoClassDefFoundError - 这意味着您的类在运行时错过(不是在构建/编译期间)。所以你应该打开项目的运行配置对话框(项目上下文菜单 - >运行方式 - >运行配置...),并确保你有bson-xxx.jar,mongodb-driver-xxx。 jar和mongodb-driver-core-xxx.jar以某种方式列在Classpath选项卡中。是的,像Xavier Bouclet说 - 如果你在应用服务器下运行它 - 这个jar应该添加到您的服务器的类路径。


I am trying to connect to a MongoDB database hosted on mlab using the Java driver on a servlet.

import org.bson.Document; 
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoConnection {

    protected void connectToMongo(String loc){

        String dbName = "readings";
        String collection = "data";

        MongoClientURI uri = new MongoClientURI("mongodb://user:pass@ds143109.mlab.com:43109/readings");
        MongoClient client = new MongoClient(uri);
        MongoDatabase db = client.getDatabase(dbName);

        MongoCollection<Document> readings = db.getCollection(collection);

        Document doc = Document.parse(loc);

        readings.insertOne(doc);

        client.close();
    }
}

The problem is I am getting the following error: java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI

I looked at one answer (How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?) that highlighted to me that I need other jars, I have since downloaded them however I am still getting this error.

I am using Eclipse and adding the three jars to the build path, navigating through the menu by right clicking on the project then following Build Path -> Configure build path -> Java build path -> libraries -> add external JARs.

Is this the right way to do it? Is there something else I am supposed to do as well/instead?

解决方案

You have java.lang.NoClassDefFoundError - that means your class is missed during runtime (not during build/compile time). So you should open your "Run Configurations" dialog for the project (project context menu -> "Run As" -> "Run Configurations...") and make sure you have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar somehow listed in Classpath tab. And yes, like Xavier Bouclet said - if you run it under application server - this jars should be added to your server's classpath.

这篇关于使用MongoDB驱动程序时java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆