MongoDB with java Exception in thread “main” java.lang.NoClassDefFoundError: org/bson/conversions/Bson

风格不统一 提交于 2020-01-23 22:16:21

问题


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

回答1:


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.

.




回答2:


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.



来源:https://stackoverflow.com/questions/40912639/mongodb-with-java-exception-in-thread-main-java-lang-noclassdeffounderror-org

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!