How to fix java.lang.NoClassDefFoundError: Could not initialize class com.mongodb.MongoClientSettings?

Website says 4.11.1 is compatible with java 8. I cannot upgrade my java version. Is there any way to fix this problem?

...
            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)) {
...

error in the try(){

java.lang.NoClassDefFoundError: Could not initialize class com.mongodb.MongoClientSettings

this is my code, It is adapted straight from the codesample on the connect via drivers page on atlas. thanks in advance!

Hi @Ham_Noobus and welcome to the community forum!!

As you have mentioned, this looks like a compatibility issue. Could you help me with the pom.xml or the gradle file configuration that you are using ?

Also, could you help me with MongoDB version you are using ?
You can also refer to the documentation on MongoClient Setting Configurations to understand further.

Regards
Aasawari

I have since resolved my issue, it seems 4.11.1 is not compatible with java 8 and has not been compatible with java 8 since version 4.6 when Jep395RecordCodecProvider was added. I believe this is a mistake on the compatibility page. I resolved my issue by downgrading to 4.5

Hi, the 4.x series of driver is compatible with Java 8. The CodecProvider you are referring to is if you wish to use Records (in which case, you would need to use JDK 16 or later as Records are not available in earlier versions of the JDK).