NoClassDefError When running Java Drivers in Docker

I have a Java based server running in Docker using openjdk:15-jdk-alpine. It connects to MongoDB Atlas using the mongodb-driver-reactivestreams driver. Similarly I have a second SpringBoot based server running in Docker that connects using the same credentials and runs just fine.

When I run my code in in IntelliJ it runs without errors, connects to MongoDB and I can query, insert documents ect…

When I put it in a Docker container and spin up the server, I get the following error message: Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.mongodb.internal.connection.tlschannel.TlsChannel

When I disable tls using tls=false in the connection string I get the message MongoSocketReadException: Prematurely reached end of stream .

In both instances, when looking through the log I can see that the server was able to identify all the shards in my MongoDB cluster, so it is connecting to Mongo Atlas and receiving information back.

Is anyone able to help debug? Im stumped as to what try next, could either be a build problem/classpath issue, a bug in the java driver, a TLS/JVM key store issue, networking issue…

2021-10-14 17:58:53.730 DEBUG c608e84d3bb5 --- [           main] u.s.PropertiesServiceImpl : Looking for profiles.

2021-10-14 17:58:53.734  INFO c608e84d3bb5 --- [           main] u.s.PropertiesServiceImpl : Profile found: dev

2021-10-14 17:58:53.773 DEBUG c608e84d3bb5 --- [           main] c.p.s.c.SocketAcceptor    : Creating server Socket at port: 6666

2021-10-14 17:58:53.788 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Connecting to Redis: localhost/6379

2021-10-14 17:58:53.872 DEBUG c608e84d3bb5 --- [           main] r.u.Loggers               : Using Slf4j logging framework

2021-10-14 17:58:54.249 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:54.764 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:55.273 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:55.783 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:56.298 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:56.810 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:57.322 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:57.834 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:58.347 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:58.857 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection failed, retrying...

2021-10-14 17:58:59.358 DEBUG c608e84d3bb5 --- [           main] s.r.s.RedisConnectionImpl : Redis connection could not be established!

2021-10-14 17:58:59.554 DEBUG c608e84d3bb5 --- [           main] .s.r.s.GameControllerImpl : Starting Room Manager

2021-10-14 17:58:59.646  INFO c608e84d3bb5 --- [           main] c.p.s.m.s.MongoDB         : Mongodb target is: mongodb+srv://XXXXX:XXXXX@XXXXXX.erble.gcp.mongodb.net/xxxxxxx?retryWrites=true&w=majority

2021-10-14 17:58:59.667 DEBUG c608e84d3bb5 --- [           main] c.p.s.m.s.MongoDB         : Starting MongoDb database...

2021-10-14 17:58:59.680  INFO c608e84d3bb5 --- [           main] o.m.d.cluster             : Cluster created with settings {hosts=[127.0.0.1:27017], srvHost=XXXXXX.erble.gcp.mongodb.net, mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', requiredReplicaSetName='XXXXXX-shard-0'}

2021-10-14 17:58:59.699 DEBUG c608e84d3bb5 --- [           main] c.p.s.m.s.MongoDB         : MongoDb client initialised.

2021-10-14 17:58:59.699 DEBUG c608e84d3bb5 --- [           main] c.p.s.m.s.MongoDB         : MongoDb client initialised.

2021-10-14 17:58:59.699 DEBUG c608e84d3bb5 --- [           main] c.p.s.Application         : Starting server...

2021-10-14 17:58:59.700 DEBUG c608e84d3bb5 --- [ AcceptorThread] c.p.s.c.SocketAcceptor    : Starting Socket acceptor loop...

2021-10-14 17:58:59.700 DEBUG c608e84d3bb5 --- [ProcessorThread] c.p.s.c.SocketProcessor   : Starting Socket processor loop...

2021-10-14 17:58:59.700 DEBUG c608e84d3bb5 --- [   ReaderThread] c.p.s.c.SocketReader      : Starting Socket reader loop...

2021-10-14 17:58:59.701 DEBUG c608e84d3bb5 --- [   WriterThread] c.p.s.c.SocketWriter      : Starting Socket writer loop...

2021-10-14 17:58:59.702 DEBUG c608e84d3bb5 --- [       Thread-5] c.p.s.c.MessageReceiver   : Starting loop.

2021-10-14 17:58:59.702 DEBUG c608e84d3bb5 --- [           main] c.p.s.m.s.MongoDB         : MongoDb client initialised.

2021-10-14 17:58:59.744  INFO c608e84d3bb5 --- [gcp.mongodb.net] o.m.d.cluster             : Adding discovered server XXXXXX-shard-00-01.erble.gcp.mongodb.net:27017 to client view of cluster

2021-10-14 17:58:59.766  INFO c608e84d3bb5 --- [gcp.mongodb.net] o.m.d.cluster             : Adding discovered server XXXXXX-shard-00-02.erble.gcp.mongodb.net:27017 to client view of cluster

2021-10-14 17:58:59.768  INFO c608e84d3bb5 --- [gcp.mongodb.net] o.m.d.cluster             : Adding discovered server XXXXXX-shard-00-00.erble.gcp.mongodb.net:27017 to client view of cluster

2021-10-14 17:58:59.770 DEBUG c608e84d3bb5 --- [gcp.mongodb.net] o.m.d.cluster             : Updating cluster description to  {type=REPLICA_SET, servers=[{address=XXXXXX-shard-00-00.erble.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, {address=XXXXXX-shard-00-01.erble.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, {address=XXXXXX-shard-00-02.erble.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}]

2021-10-14 17:59:00.093 DEBUG c608e84d3bb5 --- [ngodb.net:27017] o.m.d.connection          : Closing connection connectionId{localValue:6}

2021-10-14 17:59:00.093 DEBUG c608e84d3bb5 --- [ngodb.net:27017] o.m.d.connection          : Closing connection connectionId{localValue:2}

2021-10-14 17:59:00.094 DEBUG c608e84d3bb5 --- [ngodb.net:27017] o.m.d.connection          : Closing connection connectionId{localValue:5}

2021-10-14 17:59:00.095 DEBUG c608e84d3bb5 --- [ngodb.net:27017] o.m.d.connection          : Closing connection connectionId{localValue:3}

2021-10-14 17:59:00.095 DEBUG c608e84d3bb5 --- [ngodb.net:27017] o.m.d.connection          : Closing connection connectionId{localValue:1}

2021-10-14 17:59:00.096 DEBUG c608e84d3bb5 --- [ngodb.net:27017] o.m.d.connection          : Closing connection connectionId{localValue:4}

2021-10-14 17:59:00.095  INFO c608e84d3bb5 --- [ngodb.net:27017] o.m.d.cluster             : Exception in monitor thread while connecting to server XXXXXX-shard-00-00.erble.gcp.mongodb.net:27017

com.mongodb.MongoInternalException: Opening the TlsChannelStream failed

at com.mongodb.internal.connection.AsynchronousChannelStream$FutureAsyncCompletionHandler.get(AsynchronousChannelStream.java:328)

at com.mongodb.internal.connection.AsynchronousChannelStream$FutureAsyncCompletionHandler.getOpen(AsynchronousChannelStream.java:304)

at com.mongodb.internal.connection.AsynchronousChannelStream.open(AsynchronousChannelStream.java:125)

at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:165)

at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:195)

at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:151)

at java.base/java.lang.Thread.run(Thread.java:832)

Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.mongodb.internal.connection.tlschannel.TlsChannel

at com.mongodb.internal.connection.tlschannel.TlsChannelBuilder.<init>(TlsChannelBuilder.java:37)

at com.mongodb.internal.connection.tlschannel.ClientTlsChannel$Builder.<init>(ClientTlsChannel.java:45)

at com.mongodb.internal.connection.tlschannel.ClientTlsChannel$Builder.<init>(ClientTlsChannel.java:40)

at com.mongodb.internal.connection.tlschannel.ClientTlsChannel.newBuilder(ClientTlsChannel.java:87)

at com.mongodb.connection.TlsChannelStreamFactoryFactory$TlsChannelStream.lambda$openAsync$0(TlsChannelStreamFactoryFactory.java:236)

at com.mongodb.connection.TlsChannelStreamFactoryFactory$SelectorMonitor.lambda$start$0(TlsChannelStreamFactoryFactory.java:141)

... 1 more