gpt4 book ai didi

运行javacpp时出现java.lang.UnsatisfiedLinkError

转载 作者:行者123 更新时间:2023-11-30 07:25:43 25 4
gpt4 key购买 nike

我正在使用 javacpp 从 Java 访问 cpp。

我已经尝试过文档中提供的示例

cpp代码:

    CompletableFuture<Integer> futureInC(){
@StdFuture f = @cppDemo.futureInC();
CompletableFuture<Integer> future = new CompletableFuture<>();
f.then(int value -> future.complete(value));
return future;
}

Java代码:

import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;

@Platform(include="LegacyLibrary.h")
@Namespace("LegacyLibrary")
public class LegacyLibrary {
public static class LegacyClass extends Pointer {
static { Loader.load(); }
public LegacyClass() { allocate(); }
private native void allocate();

// to call the getter and setter functions
public native @StdString String get_property(); public native void set_property(String property);

// to access the member variable directly
public native @StdString String property(); public native void property(String property);
}

public static void main(String[] args) {
// Pointer objects allocated in Java get deallocated once they become unreachable,
// but C++ destructors can still be called in a timely fashion with Pointer.deallocate()
LegacyClass l = new LegacyClass();
l.set_property("Hello World!");
System.out.println(l.property());
}
}

如果我在 Intellij Idea 中运行 NativeLibrary.java 文件,则会收到以下错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniNativeLibrary in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:597)
at org.bytedeco.javacpp.Loader.load(Loader.java:438)
at org.bytedeco.javacpp.Loader.load(Loader.java:381)
at com.viettel.demo.NativeLibrary$NativeClass.<clinit>(NativeLibrary.java:13)
at com.viettel.demo.NativeLibrary.main(NativeLibrary.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

如何在 Intellij Idea 中运行示例 javacpp,我确实尝试按照 Readme.md 中的 guilde 使用命令行,没有问题。感谢您的支持!

最佳答案

当您的应用程序尝试加载 native 库时,会触发这组异常。在这种情况下,JVM 会同时查找PATH 环境 变量和 java.library .path 系统属性。要修复此异常,您需要为您尝试加载的库设置路径。就像你为java设置路径一样。

关于运行javacpp时出现java.lang.UnsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com