Using Tess4J on Tomcat - java

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.

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.

When does a shared library have to be on the Java classpath?

I'm trying to work with a third-party SDK on RHEL. Running this vendor's sample code (using their own provided run.sh) throws what I'll call Error 1: java.lang.UnsatisfiedLinkError: Unable to load library 'redacted': Native library (linux-x86-64/libredacted.so) not found in resource path. I add the library location to the class path, and that error disappears.
It is replaced with Error 2: Java.lang.UnsatisfiedLinkError: libodbc.so: cannot open shared object file: No such file or directory, where I obviously am missing a dependency. I install the appropriate MySQL package (which puts the .so in /lib64) and it's happy.
Now both .so files are in /lib64, which is on the Java library path, so I would assume it should find them the same. Even pointing LD_LIBRARY_PATH to the library (which the vendor had done in their script) doesn't resolve Error 1.
Why is the first library required to be on the class path when Java uses java.library.path to find shared objects?
It's worth noting that the vendor's script didn't run correctly out of the box, which makes me wonder if it's an issue with my particular RHEL installation.

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.

native libraries configuration under tomcat

I am trying to get the JMagick library working under Tomcat to do some image
translation following an upload.
Magic comes as a semi-stub library.jar archive and a native library
libJMagick.so . I have used
System. LoadLibrary ("JMagick");,
but I get errors in the catalina.out complaining about being unable to find
the library (details at the end of this message). the key clause being:
Caused by: java.lang.RuntimeException: Can't load MagickLoader (class not found) at magick.Magick.(Magick.java:25)
So where should I put the libraries, currently they are in WEB-INF/lib so that tomcat can use them
If your shared object is not installed througha package manager, put into setenv.sh LD_LIBRARY_PATH=$CATALINA_BASE/lib:$LD_LIBRARY_PATH and copy the so to that lib directory.

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

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.

Categories