我正在使用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,但没有问题。感谢您的支持!
发布于 2016-04-25 11:49:34
当你的应用程序试图加载一个本机库时,这组异常就会被触发。在这种情况下,JVM会同时查找 path环境变量和java.library.path系统property.To来修复这个异常,你需要为u试图加载的库设置path。就像你为java设置的路径一样。
发布于 2018-05-27 05:36:52
虽然我使用的是javacpp而不是IntelliJ,但我第一次使用Eclipse时遇到了完全相同的问题。
我的猜测是你的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的整个过程。
https://stackoverflow.com/questions/36831671
复制相似问题