不满意的链接错误:java.library.path中没有opencv_java249
在mac上运行一段代码时遇到了一些问题。 有人给我写了一个图像分析java应用程序,但当我试图在netbeans上运行它时,我一直遇到这个错误 运行:线程“main”java.lang中出现异常。未满足链接错误:否 位于java.library.path中的opencv_java249 loadLibrary(ClassLoader.java:1857)位于 位于的java.lang.Runtime.loadLibrary0(Runtime.java:870) java.lang.System.loadLibrary(System.java:1119)位于 image.prossing.Test.main(Test.java:28)java结果:1构建成功 (总时间:0秒) 创建netbeans项目,并添加必要的jar文件作为库。程序员告诉我下载正确的OpenCV版本,并将OpenCV.dll文件复制到我的java/jre/bin文件夹中。但是我找不到dll文件或java/jre文件夹。 我知道大多数编程都是在windows上进行的。希望有人能帮助我解决这个问题,并在我的mac上运行这个应用程序 以下是代码的第一部分,最有可能产生错误的部分:不满意的链接错误:java.library.path中没有opencv_java249,java,opencv,javacv,Java,Opencv,Javacv,在mac上运行一段代码时遇到了一些问题。 有人给我写了一个图像分析java应用程序,但当我试图在netbeans上运行它时,我一直遇到这个错误 运行:线程“main”java.lang中出现异常。未满足链接错误:否 位于java.library.path中的opencv_java249 loadLibrary(ClassLoader.java:1857)位于 位于的java.lang.Runtime.loadLibrary0(Runtime.java:870) java.lang.System.
/*
* 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代码行下方出现异常:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
您的程序正试图通过调用loadLibrary
方法中的参数名加载本机库,但找不到该库。确保本机库(opencv.dll)位于java.library.path
system属性中的一个位置,因为JVM查看这些位置以加载任何本机库(可能不包含“java/jre/bin”)
您可以在程序中打印java.library.path
,如下所示:
System.out.println(System.getProperty("java.library.path"));
您不能只将Windows库(
dll
file)放在Mac上运行,您需要先为Mac编译库(或获取库的Mac版本)
有关如何操作的提示,请参见此处:
只需将您的
opencv\u java249.dll所在的文件夹添加到路径中即可;它可能类似于C:\bin\opencv\build\java\x32
或C:\bin\opencv\build\java\x64
,具体取决于您的机器体系结构。问题是java.library.path
实际上是path
变量。在运行OSX Yosemite的mac电脑上,我将libopencv_java2412.dylib文件放到/library/java/Extensions
中,它成功了
构建opencv后,libopencv_java2412.dylib将在/build/lib中生成。查看您的opencv目录
例如:;(使用brew安装opencv3——使用java——使用python3安装
)
你会看到
libopencv_javaXXX.so opencv-XXX.jar
现在您已经有了OpenCV的本机Java库(libopencv\u javaXXX.so
),只剩下mac的动态库了。
链接到libopencv_javaXXX.so
到libopencv_javaXXX.dylib
ln -s libopencv_javaXXX.so libopencv_javaXXX.dylib
现在在IntelliJ或Eclipse中添加/usr/local/ceral/opencv3/XXX/share/OpenCV/java
作为本机库位置
或者将其添加到JVM参数中
-Djava.library.path=/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
netebans right klick项目chosew properti
选择run,working direktory,单击Browser change to opencv folder,release/lib,在花费了大量时间后,使用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();
}
在Harsh Vakharia的基础上,我尝试使用macports在mac上安装OpenCV:
sudo port install opencv +java
ls /opt/local/share/OpenCV/java
libopencv_java343.dylib opencv-343.jar
为了使用这个库,我希望能够在运行时修改库路径,这在中进行了讨论
最终得到了以下帮助器类和单元测试。该代码现在是
我参加的项目
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();
}
/**
*@见
*@抛出异常
*/
@试验
public void testNativeLibrary()引发异常{
如果(调试)
System.out.println(String.format(“尝试加载本机库%s”),
Core.NATIVE_LIBRARY_NAME));
assertTrue(NativeLibrary.getNativeLibPath().isDirectory());
assertTrue(nativellibrary.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);
}
}
package com.bitplan.opencv;
导入java.io.File;
导入java.lang.reflect.Field;
导入java.util.array;
导入org.opencv.core.core;
/**
*正确加载OpenCV NativeLibrary
*/
公共级国家图书馆{
受保护的静态文件nativeLibPath=新文件(“../lib”);
/**
*获取本机库路径
*
*@返回本机库的文件
*/
公共静态文件getNativeLibPath(){
返回nativeLibPath;
}
/**
*设置本机库路径
*
*@param pNativeLibPath
*-要使用的库路径
*/
公共静态void setNativeLibPath(文件pNativeLibPath){
nativeLibPath=pNativeLibPath;
}
/**
*获取当前库路径
*
*@返回当前库路径
*/
公共静态字符串getCurrentLibraryPath(){
返回System.getProperty(“java.library.path”);
}
/**
*将指定的路径添加到java库路径
*
*@param pathtoad
*要添加的路径
*@抛出异常
*@见
*/
公共静态void addLibraryPath(字符串路径添加)引发异常{
最终字段usrPathsField=ClassLoader.class.getDeclaredField(“usr_路径”);
usrPathsField.setAccessible(true);
//获取路径数组
最终字符串[]路径=(字符串[])usrPathsField.get(null);
//检查要添加的路径是否已存在
用于(字符串路径:路径){
if(路径等于(路径添加)){
返回;
}
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);
}
}
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);