【问题标题】:java.lang.UnsatisfiedLinkError when run javacpp运行 javacpp 时出现 java.lang.UnsatisfiedLinkError
【发布时间】:2016-04-25 03:41:14
【问题描述】:

我正在使用 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 之后的命令行没有问题。 感谢您的支持!

【问题讨论】:

标签: java javacpp


【解决方案1】:

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

【讨论】:

  • 认为如果你不使用这么多我们会发现你的答案更可读标记。一些适当的句子、大写、标点符号等会有所帮助。
【解决方案2】:

我第一次使用 javacpp 时遇到了完全相同的问题,尽管我使用的是 Eclipse 而不是 IntelliJ。

我的猜测是你的 maven 配置文件不正确,因此 gcc 编译器找不到你的源文件LegacyLibrary.h

使用以下链接作为参考配置您的 pom.xml。

https://github.com/oltzen/JavaCppExample/blob/master/pom.xml

注意第53和65行,填写正确的包名。这有助于编译器找出你的LegacyLibrary.h 在哪里。

另外,请观看此视频剪辑https://www.youtube.com/watch?v=LZrrqZLhtmw,它将引导您了解如何使用 maven 和 eclipse 运行 Javacpp 的整个过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    相关资源
    最近更新 更多