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!

2 Answers2

1

This set of exception is triggered when your application attempts to load a native library .In this case JVM is looking in both the PATH environment variable and the java.library.path system property.To fix this exception you need to set path for that library u are trying to load. Just like you set path for java.

Priyamal
  • 2,919
  • 2
  • 25
  • 52
  • I **think** that **we** would find your answer more **readable** if you didn't use so much **markup**. And some proper sentences, capitalization, punctuation and so on would help. – Stephen C Apr 25 '16 at 04:13
0

I got exactly the same problem when I first use javacpp although I was using Eclipse rather than IntelliJ.

My guess is that your maven configuration file is incorrect and hence gcc compiler cannot find your source file LegacyLibrary.h.

Configure your pom.xml using the following link as a reference.

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

Pay attention to line 53 and 65. Fill in the correct package names. This helps the compiler find out where your LegacyLibrary.h is.

Also, watch this video clip https://www.youtube.com/watch?v=LZrrqZLhtmw which walks you through the whole process how to run Javacpp with maven and eclipse.

Mark Jin
  • 2,616
  • 3
  • 25
  • 37