UnsatisfiedLinkError in exported (Eclipse) jar executable

The code works great when executed from Eclipse. I am using OpenCV 2.4.11 and JavaFX for the user interface. When I export an executable jar from Eclipse and run it from cmd, I get the following exception:

enter image description here

I followed many posts on the SO and OpenCV forums ( 1 , 2 , 3 , 4 ), but none of the answers help me.

I added OpenCV jar as a library, and the native library is linked with / build / java / x 64, as suggested in the SO answers.

Java build path

The exception occurs in System.loadLibrary (Core.Native_Library_Name), I checked Native_Library_Name, and the version of OpenCV is the same as I imported in my project.

public class CustomFrame extends Application{ @Override public void start(Stage primaryStage){ Group root = new Group(); Canvas canvas = new Canvas(1440, 840); ImageView imageView = new ImageView(); imageView.setFitHeight(canvas.getHeight()); imageView.setFitWidth(canvas.getWidth()); new FrameController().startCamera(imageView); root.getChildren().addAll(imageView, canvas); primaryStage.setScene(new Scene(root)); primaryStage.show(); } public static void main(String[] args) { // load the native OpenCV library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); launch(args); } } 

If someone thinks I missed something, let me know.

+5
source share
3 answers

UnsatisfiedLinkError is raised when an application tries to load its own library, for example

  • .so on Linux,
  • .dll on Windows or
  • .dylib on Mac

and that the library does not exist .

In particular, in order to find the required native library, the JVM will look in both the system and PATH environment variable and java.library.path .

Sometimes, if the native library has already been downloaded by the application and the same application tries to download it again, this can lead to an error as well.


How to deal with UnsatisfiedLinkError?

First of all, we need to make sure that the parameter passed in the System.loadLibrary method is correct and that the library exists. Please note that a library extension is not required. Thus, if your library has the name SampleLibrary.dll , you must pass the value of SampleLibrary as a parameter.

In addition, if the library is already loaded with your application and the application tries to download it again, UnsatisfiedLinkError will be selected by the JVM. In addition, you must make sure that the native library is present either in the java.library.path or in the PATH environment library your application. If the library has not yet been found, try specifying the absolute path to the System.loadLibrary method.

To run your application, use the -Djava.library.path argument to explicitly specify your own library. For example, using a terminal (Linux or Mac) or a command prompt (Windows), run your application by running the following command:

 java -Djava.library.path= "<path_of_your_application>" –jar <ApplicationJAR.jar> 

You have missed a valid team. Use the following

 java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java\x64" -jar BlurDetector.jar 

or

 java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java" -jar BlurDetector.jar 

instead of your team

 java -Djava.library.path="C:\Users\vivek_elango\Desktop" -jar BlurDetector.jar // you have given wrong path of your application 
+4
source

It looks like you need to add the path containing the native opencv-2411 libraries to -Djava.library.path when starting from the command line.

So something like this:

 java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java\x64" -jar BlurDetector.jar 
+2
source

In contrast to the other answers, I rather suggest that you never use absolute paths, but use relative ones. When you give your software to another user, the user will certainly not have libraries along the same path as you. By using relative paths with respect to your application, you guarantee that the software also works on other user systems, without the need to set path variables, jvm directives, and not. They don’t even need to install OpenCV if you give them the dll library this way.

Here is the code for loading the libraries in a relative way:

 public static void initOpenCv() { setLibraryPath(); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.out.println("OpenCV loaded. Version: " + Core.VERSION); } private static void setLibraryPath() { try { System.setProperty("java.library.path", "lib/x64"); Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); fieldSysPath.set(null, null); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } 

All you have to do is

  • put the libraries in the lib / x64 folder relative to your jar file.
  • in your application you need to call initOpenCv() at the beginning of your program.

What is it. In this way, you can continue to evolve and support the redistributable application.


Here is the full version:

 import java.lang.reflect.Field; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.stage.Stage; import org.opencv.core.Core; public class Main extends Application { @Override public void start(Stage primaryStage) { initOpenCv(); HBox root = new HBox(); Label infoLabel = new Label(); infoLabel.setText("OpenCV loaded. Version: " + Core.VERSION); root.getChildren().add(infoLabel); Scene scene = new Scene(root, 400, 400); primaryStage.setScene(scene); primaryStage.show(); } public static void initOpenCv() { setLibraryPath(); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.out.println("OpenCV loaded. Version: " + Core.VERSION); } private static void setLibraryPath() { try { System.setProperty("java.library.path", "lib/x64"); Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); fieldSysPath.set(null, null); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } public static void main(String[] args) { launch(args); } } 

With a folder structure like this:

 .\application.jar .\lib\x64\*.dll 

Hint: I have packaged opencv jar in application.jar

+2
source

Source: https://habr.com/ru/post/1247656/


All Articles