First of all, describe the project environment: Maven + mongodb-java-driver-3.2.2
The original project was written with Mongo 2. X driver, and then upgraded to 3. X. The first big change is user name and password verification.
This is the way to get mongoclient.
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
/**
* MongoDBUtils
* @author administration
*
*/
public class DBUtil {
/**
* Connect to mongoDB with username password (MongoCredential method)
* @param mongodbAddr
* @param databaseName
* @return
*/
public static MongoClient getMongoClientByCredent(String mongodbAddr, String databaseName){
MongoClient mongoClient;
Properties p = TDTFile.getProperAddr("db.properties");
String user = p.getProperty("username");
String pswd = p.getProperty("password");
List<ServerAddress> serverAddrList = new ArrayList<ServerAddress>();
ServerAddress serverAddress = new ServerAddress(mongodbAddr);
serverAddrList.add(serverAddress);
List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
MongoCredential credential = MongoCredential.createCredential(user, databaseName, pswd.toCharArray());
credentialList.add(credential);
mongoClient = new MongoClient(serverAddrList, credentialList);
return mongoClient;
}
/**
* Connect to mongoDB with username password (URI method)
* @param mongodbAddr
* @param databaseName
* @return
*/
public static MongoClient getMongoClientByURI(String mongodbAddr, String databaseName){
MongoClient mongoClient;
Properties p = TDTFile.getProperAddr("db.properties");
String user = p.getProperty("username");
String pswd = p.getProperty("password");
//System.out.println(user + "," + pswd);
String uri = String.format("mongodb://%s:%s@%s/%s", user, pswd, mongodbAddr, databaseName);
System.out.println(uri);
MongoClientURI mongoClientURI = new MongoClientURI(uri);
mongoClient = new MongoClient(mongoClientURI);
return mongoClient;
}
}
Two ways are no problem, run well in eclipse. But!! After packaging with MVN package, it can’t be used. After a night and a day’s analysis, I locked the root cause of the error in the statement that the command line executed the command. My sentence is like this:
java -Djava.ext.dirs=../lib Other parameters
The problem lies in the – D parameter. Baidu knows that – D is equivalent to setting external environment variables. Since my main method relies on many third-party jar packages, it seems that there is nothing wrong with it. But it ignores a problem, that is
After using the – D parameter to specify other directories, Java needs to load% Java_ The jar package in the home% \ JRE/lib/ext directory is no longer loaded!! I searched the usage of – D parameter on the Internet for a long time, but no one mentioned it.. But it’s hard for me. One day is wasted here..
Solution:
Package the required external dependent packages, including some packages such as JDK’s own encryption algorithm, into lib with Maven. Problem solving. By the way, the algorithm package required by mongodb3. X is% Java_ HOME%\jre\lib\ext\sunjce_ provider.jar
Read More:
- Twitter’s distributed self increasing ID algorithm snowflake (Java version)
- Monitoring session to determine whether the user is online or not
- keytool error: java.lang.Exception: Input not an X.509 certificate
- [Solved] JAVA connect HBase program is stuck and does not report an error
- Java Error: No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- [Solved] Error installing MVN: Java_Home is set to an invalid directory
- Idea create Maven project Error: [error] no longer supports source option 1.5. Please use version 1.6 or higher, and the idea reports an error: error: Java does not support the error of release version 5
- [How to Fix]java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x98\x82\xF0\x9F…’
- Using java to call Jython, the error URI is not hierarchical
- [Solved] Configuration Error: deployment source ‘xxx:war‘ is not valid
- [How to Solve] Java nested object @validated is not valid
- [Solved] BindingException: Type interface XXX is not known to the MapperRegistry
- [Solved] the resource is not on the build path of a java project
- Springboot2.6X version integrate knife4j error [How to Solve]
- The precision of decimal calculation with double and float in Java is not accurate
- Java error prompt….. cannot be resolved
- I/O error while reading input message; nested exception is java.io.IOException: Stream closed
- [Solved] Springboot2.x ElasticSearch Error: availableProcessors is already set to [4], rejecting [4]
- Executing Maven command error: Java_HOME is not defined correctly executing maven
- [Solved] Maven Multi-Project Compile Error: The POM for xxx is invalid