I am using Mongo DB java driver to connect to mongo instance. Below is the code I am using to create the MongoClient instance.
try {
new MongoCl
It's pretty simple and elegant:
Redirect System.err to a file:
ByteArrayOutputStream file=new ByteArrayOutputStream();
System.setErr(new PrintStream(file));
Connect to the server with your MongoClient and your MongoCredentials:
MongoCredential credenciales=MongoCredential.createCredential("root", "admin", "root".toCharArray());
MongoClient client = new MongoClient(new ServerAddress("localhost"), Arrays.asList(credenciales));
Read the error output, which is in the ByteArrayOutputStream object:
String texto=new String(file.toByteArray());
Check whether the Autentication failed string is present:
if (texto.contains("'Authentication failed.'"))
// do something;
else
...