UnsatisfiedLinkError: no opencv_java249 in java.library.path

断了今生、忘了曾经 提交于 2019-12-27 12:22:46

问题


Running into some problems making a piece of code run on my mac. Had someone write me an image analysis java app but I keep getting this error when trying to run it on netbeans.

run: Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1119) at image.prossing.Test.main(Test.java:28) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

Have the netbeans project, and added the necessary jar files as libraries. The programmer told me to download the correct OpenCV version and copy the opencv.dll file to my java/jre/bin folder. But I cannot find the dll file or the java/jre folder. I know most programming happens on windows for a reason. Hope someone can help me resolve this issue and run this application on my mac.

Here is the first part of the code, the part that is most probably creating the error:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package image.prossing;

/**
 *
 * @author Dumith Salinda
 */
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import static org.opencv.core.Core.FONT_HERSHEY_SIMPLEX;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Test {

public static void main(String[] args) {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Sorry if it's not that clear, let me know what info to add if something is missing or not clear. Would truly appreciate any help you could give. Sincerely Meir Warcel


回答1:


Look into your OpenCV directory;

For an example this; (installed using brew install opencv3 --with-java --with-python3)

/usr/local/Cellar/opencv3/XXX/share/OpenCV/java

You will see;

libopencv_javaXXX.so    opencv-XXX.jar

Now that you already have OpenCV's native library for Java (libopencv_javaXXX.so) compiled with you, the only thing left is, mac's dynamic library.

Link libopencv_javaXXX.so to libopencv_javaXXX.dylib;

ln -s libopencv_javaXXX.so libopencv_javaXXX.dylib

Now add /usr/local/Cellar/opencv3/XXX/share/OpenCV/java as Native Library Locations in IntelliJ or something similar in Eclipse.

Or add this to your JVM arguments;

-Djava.library.path=/usr/local/Cellar/opencv3/XXX/share/OpenCV/java



回答2:


On a mac running OSX Yosemite, I dropped the libopencv_java2412.dylib file into /Library/Java/Extensions and it worked.

After you build opencv, the libopencv_java2412.dylib is generated in /build/lib.




回答3:


After Spending a lots of time , and using different suggestions from StackOverflow I managed to get solution for windows. but I am adding a solution for mac as well. hope it should work.

  1. Load your lib as per your system configuration.

    private static void loadLibraries() {
    
        try {
            InputStream in = null;
            File fileOut = null;
            String osName = System.getProperty("os.name");
            String opencvpath = System.getProperty("user.dir");
            if(osName.startsWith("Windows")) {
                int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
                if(bitness == 32) {
                    opencvpath=opencvpath+"\\opencv\\x86\\";
                }
                else if (bitness == 64) { 
                    opencvpath=opencvpath+"\\opencv\\x64\\";
                } else { 
                    opencvpath=opencvpath+"\\opencv\\x86\\"; 
                }           
            } 
            else if(osName.equals("Mac OS X")){
                opencvpath = opencvpath+"Your path to .dylib";
            }
            System.out.println(opencvpath);
            System.load(opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll");
        } catch (Exception e) {
            throw new RuntimeException("Failed to load opencv native library", e);
        }
    }
    

2.now use this method as per your need

public static void main(String[] args) {
    loadLibraries();
} 



回答4:


Exception is occurring from below line of code:

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Your program is trying to load a native library by the name of argument in call to loadLibrary method, which it is not able to locate. Make sure that native library (opencv.dll) is placed at one of the locations present in java.library.path system property as JVM looks at these locations for loading any native library (which might not contain 'java/jre/bin').

You can print java.library.path in your program like below:

System.out.println(System.getProperty("java.library.path"));



回答5:


Building on Harsh Vakharia's answer i tried installing OpenCV on my mac with macports:

sudo port install opencv +java
ls /opt/local/share/OpenCV/java
libopencv_java343.dylib opencv-343.jar

To use this library I was hoping to be able to modify the library path at runtime which was discussed in

  • Adding new paths for native libraries at runtime in Java

And ended up with the following helper class and unit test. The code is now part of the

Self Driving RC-Car open Source project in which I am a comitter.

JUnit Test

/**
   * @see <a href=
   *      'https://stackoverflow.com/questions/27088934/unsatisfiedlinkerror-no-opencv-java249-in-java-library-path/35112123#35112123'>OpenCV
   *      native libraries</a>
   * @throws Exception
   */
  @Test
  public void testNativeLibrary() throws Exception {
    if (debug)
      System.out.println(String.format("trying to load native library %s",
          Core.NATIVE_LIBRARY_NAME));
    assertTrue(NativeLibrary.getNativeLibPath().isDirectory());
    assertTrue(NativeLibrary.getNativeLib().isFile());
    NativeLibrary.load();
  }

NativeLibrary

package com.bitplan.opencv;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;

import org.opencv.core.Core;

/**
 * load OpenCV NativeLibrary properly
 */
public class NativeLibrary {
  protected static File nativeLibPath = new File("../lib");

  /**
   * get the native library path
   * 
   * @return the file for the native library
   */
  public static File getNativeLibPath() {
    return nativeLibPath;
  }

  /**
   * set the native library path
   * 
   * @param pNativeLibPath
   *          - the library path to use
   */
  public static void setNativeLibPath(File pNativeLibPath) {
    nativeLibPath = pNativeLibPath;
  }

  /**
   * get the current library path
   * 
   * @return the current library path
   */
  public static String getCurrentLibraryPath() {
    return System.getProperty("java.library.path");
  }

  /**
   * Adds the specified path to the java library path
   *
   * @param pathToAdd
   *          the path to add
   * @throws Exception
   * @see <a href=
   *      'https://stackoverflow.com/questions/15409223/adding-new-paths-for-native-libraries-at-runtime-in-java'>Stackoverflow
   *      question how to add path entry to native library search path at
   *      runtime</a>
   */
  public static void addLibraryPath(String pathToAdd) throws Exception {
    final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathsField.setAccessible(true);

    // get array of paths
    final String[] paths = (String[]) usrPathsField.get(null);

    // check if the path to add is already present
    for (String path : paths) {
      if (path.equals(pathToAdd)) {
        return;
      }
    }

    // add the new path
    final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
    newPaths[newPaths.length - 1] = pathToAdd;
    usrPathsField.set(null, newPaths);
  }

  public static File getNativeLib() {
    File nativeLib = new File(getNativeLibPath(),
        "lib" + Core.NATIVE_LIBRARY_NAME + ".dylib");
    return nativeLib;
  }

  /**
   * load the native library by adding the proper library path
   * 
   * @throws Exception
   *           - if reflection access fails (e.g. in Java9/10)
   */
  public static void load() throws Exception {
    addLibraryPath(getNativeLibPath().getAbsolutePath());
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  }

}



回答6:


You cannot just put Windows library (dll file) on Mac and have it running - you need to compile the library for Mac first (or get Mac version of the library).

Please see here for tips on how to do it:

.dll Equivalent on Mac OS X

How do third-party libraries work in Objective-C and Xcode?

How to use a Windows DLL with Java in Mac OS X?




回答7:


Just add into the path the folder where your opencv_java249.dll is; it would be something like C:\bin\opencv\build\java\x32 or C:\bin\opencv\build\java\x64 depending of your machine architecture. The problem is that java.library.path is actually the path variable.




回答8:


netebans right klick project chosew properti chose run, working direktory, click Browser change to opencv folder, release/lib,



来源:https://stackoverflow.com/questions/27088934/unsatisfiedlinkerror-no-opencv-java249-in-java-library-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!