Skip to content

Could not initialize class org.nd4j.linalg.factory.Nd4j in docker container

I am trying to import a KERAS file in a docker container with a program that has the following java code sample:

String weights = getFile(_modelFile).getAbsolutePath();
String modelConfiguration = getFile(_configFile).getAbsolutePath();
_model = KerasModelImport.importKerasSequentialModelAndWeights(modelConfiguration, weights);

I am using Docker with WSL2 and the docker container has the following OS:

If I run the program in WSL2 it works fine, but if I run it in the docker container I get the following error:

2022-01-11 09:34:36,124 [ListenerHTTP-46 ] WARN xpto.jaf.protocols.json.JSONServlet JAF_G1000 [] – xpto.jaf.JafError: Failed to invoke operation method – java.lang.NoClassDefFoundError – java.lang.NoClassDefFoundError: Could not initialize class org.nd4j.linalg.factory.Nd4j at$JavaMethodOperation.execute( at at at xpto.jaf.protocols.json.JSONServlet.processOperation( at xpto.jaf.protocols.json.JSONServlet.doPost( at javax.servlet.http.HttpServlet.service( at javax.servlet.http.HttpServlet.service( at org.eclipse.jetty.servlet.ServletHolder.handle( at org.eclipse.jetty.servlet.ServletHandler.doHandle( at org.eclipse.jetty.server.handler.ScopedHandler.handle( at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle( at org.eclipse.jetty.server.handler.HandlerWrapper.handle( at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle( at org.eclipse.jetty.server.handler.ContextHandler.doHandle( at org.eclipse.jetty.server.handler.ScopedHandler.nextScope( at org.eclipse.jetty.servlet.ServletHandler.doScope( at org.eclipse.jetty.server.handler.ScopedHandler.nextScope( at org.eclipse.jetty.server.handler.ContextHandler.doScope( at org.eclipse.jetty.server.handler.ScopedHandler.handle( at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle( at org.eclipse.jetty.server.handler.HandlerWrapper.handle( at org.eclipse.jetty.server.Server.handle( at org.eclipse.jetty.server.HttpChannel.lambda$handle$1( at org.eclipse.jetty.server.HttpChannel.dispatch( at org.eclipse.jetty.server.HttpChannel.handle( at org.eclipse.jetty.server.HttpConnection.onFillable( at$ReadCallback.succeeded( at at$ at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask( at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce( at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce( at at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob( at org.eclipse.jetty.util.thread.QueuedThreadPool$ at Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.nd4j.linalg.factory.Nd4j at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet( at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet( at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.importWeights( at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.( at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.( at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildSequential( at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights( at at at at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke( at sun.reflect.DelegatingMethodAccessorImpl.invoke( at java.lang.reflect.Method.invoke( at$JavaMethodOperation.execute( … 36 common frames omitted

Nd4j is a Dl4j dependency and it is present in both executions which makes me think the problem could be the Docker itself or the OS on the docker container.

Hope to have explained my problem well. Thanks in advance.

EDIT: Both versions of Dl4j and Nd4j are 1.0.0-M1.1



This still doesn’t show a cause. NoClassDeffFoundErrors are usually related to a clashing dependencies. You could have different versions of dl4j/nd4j on your classpath but I doubt it. Most of the time this is the side effect of a native dependency crash somehow.

Of note here:

I wouldn’t recommend running the keras converter (or any model import process) in line. I would recommend converting the models separately. This is mainly for performance reasons

Whatever your problem is there are usually a few:

  1. glibc version with hdf5. Keras import uses hdf5 underneath the covers which means c code.

  2. Nd4j native dependency crash: this is also usually glibc related. We load nd4j in to memory to create and set native arrays (which means more java calling in to c++) that then can trigger a crash depending on what OS you’re running on

  3. Another hdf5 error: this could be an invalid model or some hdf5 version error.

In any case, we would need more information before we can help you. Whatever you’re reporting here isn’t enough. Could you mention your docker container OS and what version of dl4j/nd4j is bundled here?

Edit: I see it’s oracle linux 7 which is effectively RHEL/Centos. If you’re using docker I would recommend a newer image maybe.

Beyond that if it is an nd4j related crash (still not verifiable from your stack trace) if you are using the latest version you might be seeing a crash due to glibc version.

If so there was a recent update to the nd4j classifiers you can find here:

Older glibcs need to use linux-x86_64-compat as a migration path

8 People found this is helpful