UnsatisfiedLinkError: no opencv_java249 in java.library.path

54,721

Solution 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

Solution 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.

Solution 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();
} 

Solution 4

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

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);
  }

}

Solution 5

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"));
Share:
54,721

Related videos on Youtube

Meir
Author by

Meir

Updated on July 09, 2022

Comments

  • Meir
    Meir almost 2 years

    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

    • SJuan76
      SJuan76 over 9 years
      Check how to setup the path to native libraries in JNI
    • striving_coder
      striving_coder over 9 years
      So did you try to use recompiling library for Mac or getting Mac version, and if so, did it work for you?
  • striving_coder
    striving_coder over 9 years
    I think you're missing the point that Meir is trying to link to the Windows library (.dll file) while running his code on Mac.
  • saharsh-jain
    saharsh-jain over 9 years
    @striving_coder Oh..yes! it won't work with .dll on Mac. Meir needs to get library for Mac.
  • danielhadar
    danielhadar over 7 years
    Bravo! Works also on El Capitan.
  • Ben Steffan
    Ben Steffan about 7 years
    This answer doesn't seem to answer the question, which is not about Windows, but about Mac. Also, please explain what your code does and why and in which way it solves the problem.
  • Spindizzy
    Spindizzy almost 7 years
    Even on Sierra.
  • WillC
    WillC over 6 years
    -Djava.library.path= is the way to go: gives you more flexibility to compile different versions of OpenCV and choose which to run, e.g. to compare performance.
  • Vivek
    Vivek over 2 years