JNA java.lang.UnsatisfiedLinkError: Unable to load library './OUNPPM' - java

So, I have a project that uses a series of external C .dlls and it works fine when running in Netbeans, but when I try to run the .jar by itself, I get this error:
Exception in thread "Thread-3" java.lang.UnsatisfiedLinkError: Unable to load library './OUNPPM': The specified module could not be found.
I've encountered this before for a few different reasons:
1) Not finding the file.
2) Not finding another .dll that .dll is dependent on.
3) Trying to load a 64-bit .dll with the 32-bit JRE (or vice versa)
Is there any way to get a better error message to find out what is going on? JNI gave better errors, but I'm not really at a place I can change those right now.

when you are launching the main class in your jar file, how are you running it and have you set your jna.library.path.
If your jna.library.path points to the location of the correct dlls then you shoul dnot get those errors.

Related

OpenCV 4.3.0 java.lang.UnsatisfiedLinkError in Eclipse

I'm trying to do some template matching with the Java binding of OpenCV 4.3.0 in Eclipse, but attempting to load the template image always results in this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.imgcodecs.Imgcodecs.imread_0(Ljava/lang/String;I)J
The line of code where this exception is thrown is this:
flowerTemplate = Imgcodecs.imread("/templates/flowerpot_white.png", Imgcodecs.IMREAD_COLOR);
I have tried a number of solutions suggested on similar questions on StackOverflow and elsewhere on the internet, including:
Pointing at the native library folder with the "Native library location" variable in the user library definition in Eclipse.
Adding the native library folder location to my PATH variable.
Adding the native library .dll location to my PATH variable.
Setting up the Eclipse run configuration to add the native library folder & .dll locations to the PATH and CLASSPATH variables.
Loading the library with the appropriate Java code, in each of the three ways I saw it suggested, in three different places which all run before the code that throws the exception.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load(<path_to_the_dll>);
File opencvLibrary = new File(System.mapLibraryName(Core.NATIVE_LIBRARY_NAME));
System.load(opencvLibrary.getAbsolutePath());
Placing the .dll in question into my source folder and every subfolder. I am running it from within Eclipse, so this is also the program's working directory.
UnsatisfiedLinkError is a runtime exception that happens when running your Java program. So placing your file in the source folder will not work.
You need it to be available in a place that your program can find it.
See this article for example:
https://www.javaworld.com/article/2077520/java-tip-23--write-native-methods.html
In it they place the library in Linux's library path. In windows you'd similarly place it in the current directory (where you're running from) or in some shared location.
This article explains Window's dll search order: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order
You shouldn't need to explicitly call System.loadLibrary() yourself. That's the library's responsibility.
Your problem is that OpenCV is improperly installed on your machine or inaccessible from Eclipse.
For instructions on how to make in work in Eclipse see:
Add .dll to java.library.path in Eclipse/PyDev Jython project
After removing every load method and then adding them back one-by-one, I determined that the issue was most likely caused by Eclipse loading the native library folder twice.

JNI Unable to load native library

I'm using JNI as mean to connect my java code and C code.
The code compiles fine and the screen GUI application opens and then closes which means that there is nothing wrong with that part of the code which is pure C.
However when I run the program the output console immediately tells me:
Error occurred during initialization of VM
Unable to load native library: Can't find dependent libraries
and returns 1 as error code. I got the jvm.dll in the same folder of the exe and I think JDK PATH is correctly set up. I have no clue for what the problem may be.
I searched for the error but couldn't fix it.
That error means that the Java VM is finding and attempting to load your .dll, but additional .dlls that your .dll depends on cannot be found. One option is to make sure that those extra dependencies are in the PATH, but that can be tricky because it depends on setting up environment variables during installation, setting them at runtime, or placing additional .dlls into the same folder.
Another way to fix it is to use the -static linker flag when linking your .dll (note that this is not mutually exclusive with the -shared option, which tells the linker to package your code into a .dll). This means that the additional dependencies will be statically linked into your .dll, instead of needing to be located at runtime.

UnsatisfiedLinkError loading OpenCV (Linux)

I need to create a runnable jar in Windows with Eclipse and start it with Linux
I'm getting an unsatisfiedLinkError while loading OpenCv. I have edited the correct library path for a Linux computer in Eclipse and finally created a runnable jar. When I start the *.jar file in linux and get the following error.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no
opencv_java2411 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1889)
at java.lang.Runtime.loadLibrary0(Runtime.java:900)
at java.lang.System.loadLibrary(System.java:1087)
Basically i have two question.
1. How can i get the loaded library path at runtime?
2. Is it possible that the chosen way is not the right way to handle the problem?
Thanks
One important this to keep in mind for loading linux libraries with System.loadLibrary, omit the lib prefix in the library name, for instance, if the library name is libxyz.so, your call would be:
System.loadLibrary("xyz");
Set the path of OpenCV in the variable LD_LIBRARY_PATH. You can do it in the shell where you run the "jar". Use export command.

Unable to load native library java; specified module not found

I'm trying to run a piece of sample code to connect to a specific DVR. I've got the SDK from the DVR manufacturer. The code tries to call a native library like this:
DHNetSDKLib INSTANCE = (DHNetSDKLib)Native.loadLibrary("dhnetsdk", DHNetSDKLib.class);
But, no matter where I put the dll file, I always get this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'dhnetsdk': Specified module could not be found.
This being a piece of demo code, I feel this should work. The dll files are placed in the root folder as standard. I've tried copying them to the windows dll folders, gave them a folder of their own (C:\dll), adding this folder to the PATH variable, editing project properties to include the various folders to the build path, pointing to it like this:
DHNetSDKLib INSTANCE = (DHNetSDKLib)Native.loadLibrary("C:\\dll\\dhnetsdk.dll", DHNetSDKLib.class);
But nothing works. I've searched a lot but I have yet to find a solution. Most threads I come across however usually don't have the module bit in the error. Perhaps there is something wrong with the dll?
I'm trying to do this in java because I want to build an Android application using this dll, but now I'm wondering if that is even possible. The only search results I find are related to native Android code (I think). This project is being run as a standard application for testing purposes however.

Using Tess4J on Tomcat

When I use the Tess4J library I get an error:
java.lang.NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.TessAPI
at runtime.
But I don't get the meaning of this error, nor I am able to resolve it.
My problem is that 2 native dlls have to be loaded by Tess4J but this is out of my hands. I've added the location of the dlls to the build path for each jar.
I noticed, that when I first trigger Tess4J with my client, I get an error:
java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract302'
but every consecutive call results in the NoClassDefFoundErrorexception.
In my case switching from 64-bit Java to 32-bit Java solved the java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract302' problem (Tesseract is a 32-bit app). Don't forget to put libtesseract302.dll, liblept168.dll and tessdata in jdk/bin folder.

Categories