简体   繁体   中英

MongoDB java error java.lang.NoClassDefFoundError: com/mongodb/MongoClient

I'm trying to make a connection between mongodb and java, but I get an error:

java.lang.NoClassDefFoundError: com/mongodb/MongoClient

I am aware this question is asked multiple times before but none of those i've tried works

1 adding to classpath (Also, in this post they are talking about server classpath. I don't know what that is, and if that is something I have to do on the server i'm running, that's not an option)

2 I have added the all jar files also needed (bson.jar, mongodb-driver-core-jar and mongodb-java.jar) All version 3.6.3

I have tried both methods in a maven project and a java project.

When opening the jar file after its exported, in its .classpath file the jar files for mongo are listed. How can i fix my problem?

PS: I notice that the jar file does not include dependency jar files. How would the jar file work on another system where files are not present? Also, not sure if it matters, but this is a plugin for a Minecraft Server

EDIT: Here's the pom.xml dependencies:

<dependencies>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.6.3</version>
    </dependency>
</dependencies>

This should already include the bson and core jars, but doesnt work. I added them manually just in case. (Build Path -> Configure Build Path -> Add external jar) using Eclipse. I've added other jar files the same way and those do work, so I don't know why mongo won't

You use eclipse I suppose from the .classpath file.

You have mixed the maven and non-maven setup of a java project very probably.

If you don't use maven (don't have pom.xml / Disable Maven nature in eclipse) your dependencies that you have added manually to the classpath will work very probably.

If you would like to create a maven project, it is enough to define the mongodb-driver as you did, the transitive dependencies will be used as well, maven will manage them.

I think in the latter case you did in vain any jar/dependency addition to the classpath, you must use pom.xml to define your used jars/dependencies.

You have to check that the maven nature is enabled or not in eclipse: on the project there should be a 'M' to indicate that the project is a maven project. If the sign not present, please convert it using the project's context menu 'Configure' -> 'Convert to Maven Project'. You should open the Problems view in the eclipse and check it for any problem related to your project. It will tell you what you did wrong.

To answer the question how to make the project portable: If you would like to walk on the safest way, the better you can do is to build a fat jar using the maven assembly plugin - in this case the maven will pack everything into one huge jar file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM