1

I am using javacpp to access cpp from Java.

I have tried the example provided in the documentation

cpp code:

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

Java code:

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());
    }
}

I am getting following error if I run NativeLibrary.java file in Intellij Idea:

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)

How can I run example javacpp in Intellij Idea, I did try with command line following guilde in Readme.md without problem. Thank for your support!

4

2 に答える 2

1

この一連の例外は、アプリケーションがネイティブ ライブラリをロードしようとしたときにトリガーされます。この場合、JVM はPATH 環境変数と java.library.path システム プロパティの両方を参照しています。この例外を修正するには、そのライブラリのパスを設定する必要があります。あなたはロードしようとしています。Javaのパスを設定するのと同じように。

于 2016-04-25T03:49:34.057 に答える