Unsatisfied Link Error Help Java JNI Eclipse - java

Hi I am trying to run a project in eclipse and am getting the runtime error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: mywrapperclass.nativemethod()V
I only get the error code when I call a native method in the wrapper class from my main. I have looked around on many forums and blogs/websites and haven't been able to fix this. Debug and my browsing have me led me to thinking that the problem lies in the project not being able to find the native methods within the dll.
I had a lot of trouble getting eclipse to find the .dll and eventually solved this by placing the dll in the system32 folder.
Thanks in advance

Project > Properties > Java Build Path
Click on the arrow of the JAR that needs the DLL library.
Native library location
You have to specify the location of the DLL library.

Easiest way to bundle the dlls with eclipse plugin is
create a folder structure under the plugin like this "os/win32/x86" for x86 architecture and place your dlls there.
and load the libraries like this in your plugin code System.loadLibrary("Dll name");

open .classpath in the project location
Set the dll path as "value" for "CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" attribute

Related

Missing library on java.library.path trying to deploy executable JAR

I'm attempting to execute an executable JAR I generated using Eclipse for a Slick2D game project I'm trying to deploy, and I keep getting an error saying:
java.lang.UnsatisfiedLinkError: no lwjgl64 in java.library.path
I followed the Slick2D setup to the letter when setting up the project months ago. But up until now, I've always run through Eclipse, and have never attempted to run through a JAR.
To generate the JAR, I chose Export -> Runnable JAR, and I chose to package the required libraries into the JAR. I then tried to execute the JAR from the command line as so:
java -jar JAR_NAME.jar
After receiving the above error, I tried many different methods of getting this so-called lwjgl64 library onto the java.library.path, but to no avail.
So far I've tried:
Changing the native library location of lwjgl.jar in my Slick2D user library to the folder containing the lwjgl-natives-*.jar files, but those only have the 32-bit definitions in them, and I figured it was looking for a DLL, so...
I ran the following command instead:
java -jar JAR_NAME.jar -Djava.library.path="C:\slick2d"
But this produced the same result, even though that directory contains lwjgl64.dll, among other files.
I tried copying all of the dll/so files from C:\slick2d into a folder called natives in a source folder in Eclipse and using the following code to set it as the java.library.path:
System.setProperty("java.library.path", "libs");
System.setProperty("org.lwjgl.librarypath", new File("libs/natives").getAbsolutePath());
But this just resulted in yet another error:
java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\packetpirate\workspace\GZS Redux\target\libs\natives\lwjgl64.dll
Does anyone have any insight into this problem? I'm not great at building projects, especially in Eclipse, so I've always had trouble with linking dependencies. I've thought of converting my project to Maven, but I wouldn't know where to start and how to set it up.
EDIT: I realized that the libs/natives folder was not copied to the target folder when I generated my JAR, and after copying it over, I was able to run the JAR, but I still get similar errors related to jinput-dx8_64 and jinput-raw_64, which are also in the same folder. It doesn't seem to affect the game's behavior, though...
Also, although I know this is a Slick2D specific thing... it seems to be loading my assets twice. The only way I can see this happening is if the Loading state is entered twice, which doesn't make sense.

How to get JNA to extract several DLL files from a jar file?

I am working on a Java project in NetBeans using JNA. According to the JNA documentation, I can make my DLL:s available to Java by putting it in the jar:
Make your native library available on your classpath, under the path {OS}-{ARCH}/{LIBRARY}, where {OS}-{ARCH} is JNA's canonical prefix for native libraries (e.g. win32-x86, linux-amd64, or darwin). If the resource is within a jar file it will be automatically extracted when loaded.
This is what I want to do, so I have included the DLL:s in the project under src/win32-x86-64. If I build a jar-file with netbeans, and then include the jar file in another project everything works fine and JNA finds my library without a problem. This is what I get with jna.debug_load on:
Looking in classpath from sun.misc.Launcher$AppClassLoader#15db9742 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:file:/C:/MyNetBeansProject/dist/lib/jna-4.2.2.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Looking for library 'MyLibrary'
Adding paths from jna.library.path: null
Trying MyLibrary.dll
Adding system paths: []
Trying MyLibrary.dll
Looking for lib- prefix
Trying libMyLibrary.dll
Looking in classpath from sun.misc.Launcher$AppClassLoader#15db9742 for MyLibrary
Found library resource at file:/C:/MyNetBeansProject/build/classes/win32-x86-64/MyLibrary.dll
Looking in C:/MyNetBeansProject/build\classes\win32-x86-64\MyLibrary.dll
Found library 'MyLibrary' at C:/MyNetBeansProject/build\classes\win32-x86-64\MyLibrary.dll
Apparently the DLL from the jar is not used. Instead the DLL from the build folder is used.
Now, if I move the jar-file to another folder and include it in my project, I get a UnsatisfiedLinkError. JNA gives the following output:
Looking in classpath from sun.misc.Launcher$AppClassLoader#70dea4e for /com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:file:/C:/SomeFolder/lib/jna-4.2.2.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Looking for library 'MyLibrary'
Adding paths from jna.library.path: null
Trying MyLibrary.dll
Adding system paths: []
Trying MyLibrary.dll
Looking for lib- prefix
Trying libMyLibrary.dll
Looking in classpath from sun.misc.Launcher$AppClassLoader#70dea4e for MyLibrary
Found library resource at jar:file:/C:/SomeFolder/MyNetBeansProject.jar!/win32-x86-64/MyLibrary.dll
It looks like JNA finds the DLL in the jar, but it does not try to extract it. I can not find it in my temp folder (where JNA extracts it's own internal DLL).
What is the problem here? Why doesn't JNA extract the file? How can I fix this?
(I don't know if this is relevant, but I should mention that my DLL depends on multiple other DLL files that are in the same folder in the jar. Not sure if JNA will extract them automatically for me, but so far it seems JNA isn't even extracting the DLL I am actually using.)
EDIT: There seems to be no problem locating jnidispatch.dll. According to the output when jna.debug_load.jna is set to true the file is found in the JAR and extracted to the temp folder.
JNA show error "UnsatisfiedLinkError" when dll couldn't be loaded. If your DLL needs another custom DLLs not present in the system path it will fail, as JNA doesn't extract this dll automatically.
JNA as a Java library doesn't know the dependencies of the system library, so it can't extract from the jar. The solution is to specify all the dependencies in the JNA Java interfaces.
You can see an example here Load multiple dependent libraries with JNA
What is happening behind the scenes of the Operating System
At the end, the libraries are loaded by the operating system as requested by the main executable. In this case the main executable is java.exe or (jvm.dll). If the system can't find a library in the path it fails and java generates an exception.
Another related and solved question is Registering multiple .dll libraries into a single java class using JNA

Referenced Library in Java Project only finds native dll when I specify where they are in IDE

I have a library in eclipse that uses native libraries (two dll). The dlls are in the same folder as the jar. When I run the application there is an error:
java.lang.UnsatisfiedLinkError: no xx in java.library.path
When I link the library by double click on native library location, then running it works. However, I don't want to have to do this, I want it to work with out having to configure it via the IDE. I want the library to be plug and play.
Q1: Where is the convetional location to put .dll referenced by a .jar library.
Q2: How can I make sure when someone references my library in eclipse it works without further IDE configuration?
Error explains it clearly that your native libraries are not in the default location of java.library.path
You just have to set property java.library.path to the folder where all native libraries are stored. You can do this by giving argument in the command line -Djava.library.path=C:/nativeLibs/
OR
Copy your libraries to default location of java.library.path
Refer Default Java library path? for default location of java.library.path

Matlab JavaBuilder jar in Eclipse- missing mkl.dll?

I've been given some jars generated by the Matlab JavaBuilder by some colleagues. Some work fine when I use them in my Eclipse project, but a new one fails with this error:
libmwblas: load error: mkl.dll
And a message about the dll not being found. When I look in the Matlab Compiler Runtime bin folder there is a mkl.dll in there.
Any suggestions as to why Matlab can't or won't see the dll?
Your JARs are using JNI and trying to resolve the required DLL.
Option one is to resolve this is by adding /bin folder you mentioned to your Windows PATH so that Java is able to resolve this (and probably some other) DLLs.
Another (cleaner for development purposes) option is to configure Eclipse by specifying Native library location for the JAR which attempts to load DLLs. Go to Java Build Path of your project, then find the appropriate JAR in Libraries tab, then expand it and set Native library location.

Opencv Java exception

I'm using Windows 7 and getting this exception when trying to run a Java project that uses opencv libraries:
Exception in thread "main"
java.lang.UnsatisfiedLinkError: no opencv_java in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.opencv.highgui.Highgui.<clinit>(Highgui.java:416)
at teste.main(teste.java:21)
What did I do wrong? Is some import missing?
I want to create a simple Java project in Eclipse (not Android), that uses openCV.
So I've extracted javacv from OpenCV-2.4.2.exe file to C:\
Then executed "cmake -G "MinGW Makefiles" -DBUILD_opencv_java=ON
C:\opencv" command and after that, "mingw32-make". Everything was
build without errors or warnings
After I've added opencv dll's to my Environment Variables
This exception is raised because the system is trying to find native libraries that OpenCV needs for this specific platform, and does not find them.
Exception in thread "main"
java.lang.UnsatisfiedLinkError: no opencv_java in java.
To solve this error:
If you are using opencv version < opencv 2.4.6 then the answer given by #user3033420 is the solution.
If you are using version >= opencv 2.4.6, then the jar has a constant variable in Core class named NATIVE_LIBRARY_NAME that you give to the loadLibrary() function in FaceDetector class to include features of opencv in your project, you probably already have this:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Using the above named constant, there is no need to remember name of strang dll files.
Add the opencv 2.4.9.jar to the build path from project preferences as:
Window -> Preferences -> Java Build Path –> Add Library -> User Libraries ->
User Library -> & click New
You will see a dialog as below. Add opencv-2.4.9 as Library Name & click Ok.
Then Add External Jar & locate your opencv jar & click ok. Then expand opencv-2.4.9.jar & click on Native Library Location (None) as shown below :
Necessary step: enter the location of the folder containing native library used by "opencv-2.4.9" & then click OK as shown below :
So now the jar now has all the native libraries it needs to do the work. Rebuild the java program and everything should compile and run as designed.
If you get this error:
Exception in thread "main"
java.lang.UnsatisfiedLinkError: no opencv_java in java.library.path
It probably means you are shooting from the hip, programming by brownian motion, trying to get openCV to work. Like trying to figure out how an airplane works in flight by pressing all the buttons furiously. You're going to have a bad time.
What the error means:
Eclipse is telling you that the jar file can't find libraries it needs to do its job. So naturally it's not going to work until you make them available. You've got to find a tutorial on "how to build openCV from source" on your particular platform: (windows, mac, linux, etc), (32bit, 64bit, etc).
Basically, you glossed over the 'Native library location' settings, or didn't set them correctly, and so the jar can't find its support libraries written in C.
How do fix it, thousand foot view:
Download the source code for openCV for your operating system.
Follow the directions to build openCV from source.
Copy the jar into a lib directory in your Java project.
Configure the jar to look for its native libraries by setting the "native library location" to the build/lib directory under the path where you built openCV from source.
Clean build the java project, and the UnsatisfiedLinkError should go away.
This blog talks about the steps above in step-by-step detail: https://udallascs.wordpress.com/2014/03/30/adding-opencv-and-configuring-to-work-with-eclipse-and-java/
Why can't this just be a simple jar?
Because most of openCV is written in the C programming language. And the jar file you are using is just a window into that C world. So it's a rube Goldberg machine. You'll see these sorts of things all over the place in the real work world, so pay attention, you are getting an education here.
Trying to load the library by doing this:
System.loadLibrary("opencv_java244")
Or by doing this:
System.loadLibrary("opencv_java244")
Didn't work - still got the same error.
Finally what worked was providing the full path to the dylib file and using:
System.load(new File("/usr/local/Cellar/opencv/2.4.10.1/share/OpenCV/java/libopencv_java2410.dylib").getAbsolutePath());'
I'm using HomeBrew but however you installed it just find the file and uddate the path.
I find solution. Actual dll is located at openCV\opencv\build\java\x64\ folder. In my case its name is opencv_java247.dll , So i have changed java code line
System.loadLibrary("opencv_java244")
to
System.loadLibrary("opencv_java247")
I also set native library location as E:/Sagar_tools/tools/openCV/opencv/build/java/x64 (full path to dll) in build path.
The function loadlibrary tries to find the name of the the DLL in your PATH variable -- check the DLL name. You can also try System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
In that case, you should give the path for jvm to where the opencv dll file is intalled as : -Djava.library.path="C:\opencv\build\java\x64"
and add the code as :
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
As, i try this in my netbeans ide and my problem is solved.

Categories

Resources