遇到一些问题,在我的mac上运行一段代码。 有人给我写了一个图像分析java应用程序,但是当我尝试在netbeans上运行它时,我一直收到这个错误。
run:线程“main”中的异常java.lang.UnsatisfiedLinkError:no java.library.path中的opencv_java249 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结果:1构建成功 (总时间:0秒)
拥有netbeans项目,并将必要的jar文件添加为库。程序员告诉我下载正确的OpenCV版本并将opencv.dll文件复制到我的java / jre / bin文件夹中。但我找不到dll文件或java / jre文件夹。 我知道大多数编程都出现在Windows上是有原因的。希望有人可以帮我解决这个问题并在我的Mac上运行这个应用程序。
以下是代码的第一部分,即最有可能创建错误的部分:
/*
* 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);
如果不是很清楚,请告诉我,如果遗漏或不清楚,请告知我要添加哪些信息。 真的很感激你能给予的任何帮助。诚挚 Meir Warcel
答案 0 :(得分:17)
查看您的OpenCV目录;
举个例子; (使用brew install opencv3 --with-java --with-python3
安装)
/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
你会看到;
libopencv_javaXXX.so opencv-XXX.jar
既然您已经拥有OpenCV的Java本地库(libopencv_javaXXX.so
),那么唯一剩下的就是mac的动态库 强>
将libopencv_javaXXX.so
链接到libopencv_javaXXX.dylib
;
ln -s libopencv_javaXXX.so libopencv_javaXXX.dylib
现在在 IntelliJ 中添加/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
作为本地库位置或类似的东西。
或者将其添加到您的JVM参数中;
-Djava.library.path=/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
答案 1 :(得分:7)
在运行OSX Yosemite的mac上,我将libopencv_java2412.dylib文件删除到/Library/Java/Extensions
并且它有效。
构建opencv后,在/ build / lib中生成libopencv_java2412.dylib。
答案 2 :(得分:3)
花了很多时间,并使用StackOverflow的不同建议,我设法获得了Windows的解决方案。但我也在为mac添加一个解决方案。希望它能够奏效。
根据您的系统配置加载您的lib。
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.现在根据需要使用此方法
public static void main(String[] args) {
loadLibraries();
}
答案 3 :(得分:2)
从下面的代码行发生异常:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
您的程序正在尝试通过调用loadLibrary
方法的参数名称加载本机库,而该方法无法找到。确保将本机库(opencv.dll)放置在java.library.path
系统属性中的一个位置,因为JVM会查看这些位置以加载任何本机库(可能不包含'java / jre / bin')
您可以在程序中打印java.library.path
,如下所示:
System.out.println(System.getProperty("java.library.path"));
答案 4 :(得分:2)
我在严苛的Vakharia answer的基础上,尝试通过macports在Mac上安装OpenCV:
sudo port install opencv +java
ls /opt/local/share/OpenCV/java
libopencv_java343.dylib opencv-343.jar
要使用此库,我希望能够在运行时修改库路径,这已在
中进行了讨论。最后完成了以下帮助程序类和单元测试。该代码现在是
的一部分 我是通勤者的Self Driving RC-Car open Source项目。
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();
}
本地图书馆
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);
}
}
答案 5 :(得分:1)
您不能只将Windows库(dll
文件)放在Mac上并让它运行 - 您需要先为Mac编译库(或获取该库的Mac版本)。
请参阅此处获取有关如何操作的提示:
答案 6 :(得分:0)
建议您使用由OpenPnP(https://github.com/openpnp/opencv)打包的OpenCV Java库,该库包含所有必需的DLL,而不是费力地手动安装OpenCV库。
除了将其添加到您的构建自动化工具配置(在我的情况下为Gradle)并添加以下代码以加载库之外,不需要其他步骤:
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
答案 7 :(得分:-1)
只需将C:\bin\opencv\build\java\x32
所在的文件夹添加到路径中即可;它会像C:\bin\opencv\build\java\x64
或java.library.path
,具体取决于您的机器架构。问题是path
实际上是{{1}}变量。
答案 8 :(得分:-1)
netebans右键klick项目选择了propertyi 选择run,working direktory,单击Browser change to opencv folder,release / lib,