简体   繁体   中英

I keep getting “error package com.mongodb does not exist” when trying to compile MongoDB Utilites class

I am running a webapp using a connection to MongoDB where products reviews are stored. The current version of the webapp works correctly. (ie it writes and stores new reviews in the MongoDB collection).

Nonetheless, when I make changes and try to compile a new version of the MongoDB Utilities class I keep getting:

error package com.mongodb.XXX does not exist
import com.mongodb.BasicDBObject;
                  ^

I do have the following .jar files in the \\lib directory:

mongodb-driver-3.6.3
mongodb-driver-core-3.6.3
mongodb-java-driver-3.6.3
bson-3.6.3

and I mentioned them in the CLASSPATH variable:

set CLASSPATH=.;C:\apache-tomcat-7.0.34\lib\servlet-api.jar;C:\apache-tomcat-7.0.34\lib\jsp-api.jar;C:\apache-tomcat-7.0.34\lib\el-api.jar;C:\apache-tomcat-7.0.34\lib\commons-beanutils-1.8.3.jar; C:\apache-tomcat-7.0.34\lib\mongo-java-driver-3.6.3.jar; C:\apache-tomcat-7.0.34\lib\bson-3.6.3;C:\apache-tomcat-7.0.34\lib\mongodb-driver-3.6.3; C:\apache-tomcat-7.0.34\lib\mongodb-driver-core-3.6.3;  C:\apache-tomcat-7.0.34\lib\mysql-connector-java-5.1.38-bin.jar;

What am I doing wrong? How can I get java to compile my new MongoDB Utilities class?

I couldn't reproduce the same behaviour, but I only reference mongodb-java-driver-3.6.3 in my build scripts.

Since the mongodb-java-driver is an uber JAR that contains mongodb-driver , mongodb-driver-core , and bson , you could try removing these latter three from your classpath and build scripts and see if that resolves the issue.

I was able to solve my issue so I post this answer in case someone is stuck in the same problem.

Thanks to what user "nos" posts in the answer to this question I used the -verbose option when compiling eg:

javac -verbose className.java

In the errors log I noticed that the Java compiler was searching for the MongoDB classes in a different \\lib folder than the one I used in my CLASSPATH definition. So I added a copy of the mongodb-java-driver there and the compilation worked.

As craigcaulfield correctly mentions above there is no need to add the other drivers ( mongodb-driver , mongodb-driver-core , and bson ).

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