0
MongoCredential credential = MongoCredential.createCredential(user, database, password);
MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build();
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(credential), options);

throwing the following error

Failed to get process identifier from JMX, using random number instead
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
    at org.bson.types.ObjectId.createProcessIdentifier(ObjectId.java:506)
    at org.bson.types.ObjectId.<clinit>(ObjectId.java:464)
    at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47)
    at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:69)
    at com.mongodb.Mongo.createCluster(Mongo.java:683)
    at com.mongodb.Mongo.createCluster(Mongo.java:669)
    at com.mongodb.Mongo.<init>(Mongo.java:282)
    at com.mongodb.MongoClient.<init>(MongoClient.java:187)
    at com.example.michael.version_10.Connection.doInBackground(Connection.java:40)
    at com.example.michael.version_10.Connection.doInBackground(Connection.java:30)
    at android.os.AsyncTask$2.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList[[zip file "/data/app/com.example.michael.version_10-1/base.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.michael.version_10-1/lib/x86, /system/lib, /vendor/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at org.bson.types.ObjectId.createProcessIdentifier(ObjectId.java:506) 
    at org.bson.types.ObjectId.<clinit>(ObjectId.java:464) 
    at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47) 
    at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:69) 
    at com.mongodb.Mongo.createCluster(Mongo.java:683) 
    at com.mongodb.Mongo.createCluster(Mongo.java:669) 
    at com.mongodb.Mongo.<init>(Mongo.java:282) 
    at com.mongodb.MongoClient.<init>(MongoClient.java:187) 
    at com.example.michael.version_10.Connection.doInBackground(Connection.java:40) 
    at com.example.michael.version_10.Connection.doInBackground(Connection.java:30) 
    at android.os.AsyncTask$2.call(AsyncTask.java:305) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:761) 
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Kinuthia
  • 61
  • 10
  • Take a look at this solution [Link](https://stackoverflow.com/a/32776998/2993388) – bbadawee Jul 05 '17 at 12:50
  • I have forked as in the example above but my main issue seems to come from: Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList[[zip file "/data/app/ Which i have tried to soleve with no avail. – Kinuthia Jul 05 '17 at 12:56
  • are you see this link https://stackoverflow.com/questions/32529484/mongodb-3-x-driver-android-compatibility – Med Elgarnaoui Jul 05 '17 at 13:43
  • @MohamedElgarnaoui I have tried that solution but still doesn't work same problem – Kinuthia Jul 06 '17 at 13:10
  • I was actually doing it the wrong way. It is not advisable to connect to MongoDB directly it is recommended to use an API. And that solved my problem. – Kinuthia Jul 07 '17 at 08:40

1 Answers1

1

I was actually doing it the wrong way. It is not advisable to connect to MongoDB directly it is recommended to use an Rest API. And that solved my problem.

Kinuthia
  • 61
  • 10