2

When I execute mycode from intelliJ Idea it works fine but when I run the jar on my server it throws the below error. I am using 'org.mongodb:mongodb-driver:3.4.1' version.

 Exception in thread "main" java.lang.NoClassDefFoundError: org/bson/conversions/Bson
        at com.myapp.server.mongo.MongoDbHandler.prepareMongoDb(MongoDbHandler.java:68)
        at com.myapp.server.mongo.MongoDbHandler.<init>(MongoDbHandler.java:22)
        at com.myapp.server.Client.main(Client.java:167)
Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson
        at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 3 more
7
  • Could you please add more information? Are you referring to a web server or an application server or your mongo server? What Java version you are using? Is it a maven build? Do you have the same Java version on the server as on your intelliJ Idea? Commented Dec 1, 2016 at 14:11
  • Most probably a dependency issue. Would make sense to post a part of your pom.xml or build.gradle, or ivy.xml, or wherever you define the dependencies. Commented Dec 1, 2016 at 14:13
  • i am using build.gradle with server java 1.8 version and intellij with 1.7 java version Commented Dec 1, 2016 at 14:21
  • put " compile group: 'org.mongodb', name: 'bson', version: '2.5.1' " under dependancies in your gradle build file; sounds like that'll fix you up Commented Dec 1, 2016 at 14:23
  • @Hans i just tried with your solution with bson version 2.5.1 but getting error as Exception in thread "main" java.lang.NoClassDefFoundError: org/bson/conversions/Bson Commented Dec 1, 2016 at 14:28

2 Answers 2

2

I know its an old thread and its just for those who have the similar issues. Few days back I was also facing the same issue with same error trace while running the jar by command line.After rechecking the code found that

It was not a problem of the jar version but its problem of the packaging of the jar

After packing the jar with all proper dependencies will solve this issue.

I have used

maven-assembly-plugin

for packaging and its works.

.

Sign up to request clarification or add additional context in comments.

Comments

0

If I had to guess you need to get yourself whatever BSON library you're using; I wouldn't say that Mongo is your problem.

key line here is: "Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson"

So it's saying that it's not finding that particular class, I would look in IntelliJ and see what BSON library it is, then put it in your build directory (or java lib) on your server.

3 Comments

i tried this solution earlier with different version of bson library but i am getting same result. i have updated my mongo library from 2.10.0 to 3.4.0 then i am facing this issue
you shouldn't have to change the version, it's probably a case of not being in the directory you/java think it should be. The easiest way to check would be to put it in the same directory as your jar and import <insert file name here of bson library> (ie import bson3.2.1; exclude the file extension) that should let you see if it's a path issue.
but from 2.10.0 version few methods are get deprecated so i updated with recent version 3.4.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.