I try to use the mongo DB atlas for my app. It works fine on the iOS side with MongoKitten.
On the Android side, I try to use the official driver
no matter which version, my app crashed when trying to connect to the database
`java.lang.NoClassDefFoundError: Failed resolution of:
Ljavax/naming/directory/InitialDirContext;
at com.mongodb.internal.dns.JndiDnsClient.createDnsDirContext(JndiDnsClient.java:73)`
`the official code
package fundamentals;
`your text`import com.mongodb.*;
import org.bson.BsonDocument;
import org.bson.BsonInt64;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
public class MongoClientConnectionExample {
public static void main(String[] args) {
// Replace the placeholder with your Atlas connection string
String uri = "<connection string>";
// Construct a ServerApi instance using the ServerApi.builder() method
ServerApi serverApi = ServerApi.builder()
.version(ServerApiVersion.V1)
.build();
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(uri))
.serverApi(serverApi)
.build();
// Create a new client and connect to the server
try (MongoClient mongoClient = MongoClients.create(settings)) {
MongoDatabase database = mongoClient.getDatabase("admin");
try {
// Send a ping to confirm a successful connection
Bson command = new BsonDocument("ping", new BsonInt64(1));
Document commandResult = database.runCommand(command);
System.out.println("Pinged your deployment. You successfully connected to
MongoDB!");
} catch (MongoException me) {
System.err.println(me);
}
}
}
}
Version:
implementation 'org.mongodb:mongodb-driver-sync:4.11.1'
also when installing
2 files found with path ‘META-INF/native-image/org.mongodb/bson/native-image.properties’.
issue comes up
Which I solve by using
pickFirst 'META-INF/native-image/org.mongodb/bson/native-image.properties
can anyone help me to solve these issues?
I tried the official guide