UnsatisfiedLinkError:java.library.path 中没有 opencv_java249
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27088934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnsatisfiedLinkError: no opencv_java249 in java.library.path
提问by Meir
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.
在我的 mac 上运行一段代码时遇到了一些问题。有人给我写了一个图像分析 java 应用程序,但是当我尝试在 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)
运行:线程“main”中的异常 java.lang.UnsatisfiedLinkError:在 java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857) 的 java.library.path 中没有 opencv_java249 在 java.lang.Runtime.loadLibrary0(Runtime.java: 870) 在 java.lang.System.loadLibrary(System.java:1119) at image.prossing.Test.main(Test.java:28) Java 结果:1 构建成功(总时间:0 秒)
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.
有了 netbeans 项目,并添加了必要的 jar 文件作为库。程序员告诉我下载正确的 OpenCV 版本并将 opencv.dll 文件复制到我的 java/jre/bin 文件夹中。但是我找不到 dll 文件或 java/jre 文件夹。我知道大多数编程发生在 Windows 上是有原因的。希望有人能帮我解决这个问题并在我的 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
抱歉,如果不是很清楚,请告诉我如果缺少或不清楚要添加哪些信息。非常感谢您能提供的任何帮助。真诚的梅尔瓦塞尔
回答by sjain
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').
您的程序正在尝试通过调用loadLibrary
方法中的参数名称加载本机库,但它无法定位。确保本机库 (opencv.dll) 位于java.library.path
系统属性中存在的位置之一,因为 JVM 查看这些位置以加载任何本机库(可能不包含“java/jre/bin”)。
You can print java.library.path
in your program like below:
您可以java.library.path
在您的程序中打印,如下所示:
System.out.println(System.getProperty("java.library.path"));
回答by striving_coder
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).
您不能只是将 Windows 库(dll
文件)放在 Mac 上并让它运行 - 您需要先为 Mac 编译库(或获取库的 Mac 版本)。
Please see here for tips on how to do it:
请参阅此处获取有关如何操作的提示:
How do third-party libraries work in Objective-C and Xcode?
第三方库如何在 Objective-C 和 Xcode 中工作?
回答by EliuX
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.
只需将您所在的文件夹添加到路径opencv_java249.dll
中即可;它类似于C:\bin\opencv\build\java\x32
或C:\bin\opencv\build\java\x64
取决于您的机器架构。问题是这java.library.path
实际上是path
变量。
回答by BatteryAcid
On a mac running OSX Yosemite, I dropped the libopencv_java2412.dylib file into /Library/Java/Extensions
and it worked.
在运行 OSX Yosemite 的 mac 上,我将 libopencv_java2412.dylib 文件放入/Library/Java/Extensions
并运行。
After you build opencv, the libopencv_java2412.dylib is generated in /build/lib.
构建 opencv 后,libopencv_java2412.dylib 会在 /build/lib 中生成。
回答by Harsh Vakharia
Look into your OpenCV directory;
查看您的 OpenCV 目录;
For an example this; (installed using brew install opencv3 --with-java --with-python3
)
例如这个; (安装使用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.
现在您已经libopencv_javaXXX.so
编译好了OpenCV 的 Java 原生库 ( ),剩下的就是 mac 的动态库。
Link libopencv_javaXXX.so
to libopencv_javaXXX.dylib
;
链接libopencv_javaXXX.so
到libopencv_javaXXX.dylib
;
ln -s libopencv_javaXXX.so libopencv_javaXXX.dylib
Now add /usr/local/Cellar/opencv3/XXX/share/OpenCV/java
as Native Library Locationsin IntelliJor something similar in Eclipse.
现在在IntelliJ或 Eclipse 中添加/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
为本地库位置。
Or add this to your JVM arguments;
或者将其添加到您的 JVM 参数中;
-Djava.library.path=/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
回答by israta sibuea
netebans right klick project chosew properti chose run, working direktory, click Browser change to opencv folder, release/lib,
neteban 右键单击项目选择了属性选择了运行,工作目录,单击浏览器更改为 opencv 文件夹,发布/lib,
回答by Kuldeep Kala
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.
在花费大量时间并使用 StackOverflow 的不同建议之后,我设法获得了适用于 Windows 的解决方案。但我也在为 mac 添加一个解决方案。希望它应该工作。
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); } }
根据您的系统配置加载您的库。
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
2.现在根据您的需要使用此方法
public static void main(String[] args) {
loadLibraries();
}
回答by Wolfgang Fahl
Building on Harsh Vakharia's answeri tried installing OpenCV on my mac with macports:
基于 Harsh Vakharia 的回答,我尝试使用 macports 在我的 mac 上安装 OpenCV:
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 Sourceproject in which I am a comitter.
自动驾驶 RC-Car 开源项目,我是其中的一名提交者。
JUnit Test
JUnit 测试
/**
* @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);
}
}