0

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

Sommervold
  • 1
  • 1
  • 2
  • It's certainly been asked many times before, and it usually comes back to the same problem. How did you actually install these jars? I see a maven tag on the question but did you actually use it? If you did then you should be able to show a pom.xml that you have and tell us which IDE you are using if any. Most IDE's will handle a lot of this for you and the common problem is more often than not a manual download and manual extraction of files instead of letting the package manager do it for you. As soon as you start talking about "missing dependcies" then it's usually a sign you did it manual – Neil Lunn May 31 '18 at 09:08
  • Please use the [edit link](https://stackoverflow.com/posts/50620504/edit) on your question to add details. Don't attempt to add them in comments. – Neil Lunn May 31 '18 at 09:24
  • I edited the post now, didn't think of editing the post. – Sommervold May 31 '18 at 09:33

1 Answers1

0

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.

m4gic
  • 1,461
  • 12
  • 19
  • I have created a whole new maven project and only copied in 3 .java files. instead of adding the other jars i need manually, those are now in pom.xml. Still does not work. I checked the problems view, no errors. when trying to debug in eclipse, eclipse said it couldnt find main method. Is it looking for the main() method? because that does not exist in my plugin. Regarding portability, I tried to add the maven plugin but cound not figure out how to export fat jar, so someone said i can export as Runnable Jar, but that does for some reason not work because its being run on a Mincraft server. – Sommervold May 31 '18 at 15:27
  • You are talking about two different way of use: you would like to run (test) it locally and run it on Minecraft server as plugin. If you would like to test it, you have two options: either you add a new class with a main method where you instantiate and call your functionality, or you have to create junit test(s) where you can do the same. Without a main method, your code could not run alone. The other option is that you test it in "production" (put it to the Minecraft server and see how it works or not). Or if you have a test Minecraft server, you can do it there. – m4gic May 31 '18 at 15:33
  • I have tested it on a minecraft server hosted locally. That is where i get the error nodefclassfound. – Sommervold May 31 '18 at 15:39
  • OK, than we are a bit further. You have to make a fat jar in order to get it work properly (using maven-assembly-plugin), or you have to figure out how to give more than one jar to a Minecraft plugin. – m4gic May 31 '18 at 16:34
  • I managed to make a fat jar file. I am not sure if it works because now I have another problem. The plugin.yml file needed for the plugin to start is missing according to the server. This file is present in project. Maybe its not included in the fat jar build? if so, how do I add it? or is it just for some reason not readable by the server? I might ask this question over at spigotmc.org if you do not know how to fix the issue. Thanks for your help anyways! – Sommervold May 31 '18 at 16:58
  • You haver to create an assembly descriptor As described here https://stackoverflow.com/questions/4602365/maven-how-to-include-specific-folder-or-file-when-assemblying-project-depending – m4gic May 31 '18 at 22:07