I need to use the onnxruntime with Java and spring boot to work with mkask detection model. but I have the below exception when calling OrtEnvironment env = OrtEnvironment.getEnvironment()
I tried to add -Djava.library.path="C:\Users\rnassar\onnxruntime.dll" to load the onnxruntime.dll with no hope.
java.lang.UnsatisfiedLinkError: C:\Users\rnassar\AppData\Local\Temp\onnxruntime-java10600223397968714237\onnxruntime.dll: A dynamic link library (DLL) initialization routine failed
at java.base/jdk.internal.loader.NativeLibraries.load(Native Method) ~[na:na]
at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:331) ~[na:na]
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:197) ~[na:na]
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:139) ~[na:na]
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2418) ~[na:na]
at java.base/java.lang.Runtime.load0(Runtime.java:852) ~[na:na]
at java.base/java.lang.System.load(System.java:2025) ~[na:na]
at ai.onnxruntime.OnnxRuntime.load(OnnxRuntime.java:387) ~[onnxruntime-1.22.0.jar:1.22.0]
at ai.onnxruntime.OnnxRuntime.init(OnnxRuntime.java:166) ~[onnxruntime-1.22.0.jar:1.22.0]
at ai.onnxruntime.OrtEnvironment.<clinit>(OrtEnvironment.java:34) ~[onnxruntime-1.22.0.jar:1.22.0]
at org.mask.onnxface.MaskDetectionService.detectMask(MaskDetectionService.java:28) ~[classes/:na]
at org.mask.onnxface.MaskDetectionController.detectMask(MaskDetectionController.java:25) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:258) ~[spring-web-6.2.6.jar:6.2.6]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:191) ~[spring-web-6.2.6.jar:6.2.6]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.2.6.jar:6.2.6]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:986) ~[spring-webmvc-6.2.6.jar:6.2.6]
3 Answers 3
I'm not sure about my solution, but in my environment very important jdk version. I mean not all jdk version works with onnxruntime. In my environment: windows 11 and onnx 1.22, 1.21, 1.20 corectly works with jdk 17 and high (Only). If I try using jdk less 17 then pop up message like yours. If i want use jdk 11 I have to use onnxruntime 1.19.2 and less. For Ubuntu not checked.
Comments
I had the same issue with OpenJDK, Windows and ONNX (multiple versions), and the reason was that the Microsoft Visual Studio Runtime shipped with the official OpenJDK build was too old.
I switched from the official OpenJDK build to the Microsoft OpenJDK 21.0.8 build and it works like a charm:
https://learn.microsoft.com/en-us/java/openjdk/download
https://aka.ms/download-jdk/microsoft-jdk-21.0.8-windows-x64.zip
(probably Microsoft uses a more current toolchain for their builds)
The complete working config is ONNX 1.22.0, Microsoft Visual Studio Redistributable v14.44.35211.0, CUDA 12.9.1, CuDNN 9.12.0
Comments
For my setup (Java 23 and 24), ONNX runtime version >= 1.21 are throwing the same error.
This is due to an incompatibility in VC runtime versions betweek the OpenJDK (on which most JREs are based) and the one needed by the ONNX runtime version (see comment on discussion).
ONNX runtime v1.20.0 is working fine.
Comments
Explore related questions
See similar questions with these tags.
java.library.pathshould define paths and not files; so-Djava.library.path="C:\Users\rnassar"(without the .dll file).