I'm currently trying to call some C++ code from Java using JNI. To understand how this would work, I followed a tutorial. I have had some hiccups, but now I am almost there. So far, I have successfully created the Java Class, implemented a method in C++, compiled the code from my windows cmd, and created the library. The last thing I'm struggling with and cannot seem to figure out is how to correctly refer to the library when running the code from the command line.
My folder structure is as follows
.
├── src
└── main
└── java
└── com
└── baeldung
└── jni
└── com_baeldung_jni_HelloWorldJNI.cpp
└── com_baeldung_jni_HelloWorldJNI.cpp~
└── com_baeldung_jni_HelloWorldJNI.h
└── com_baeldung_jni_HelloWorldJNI.o
└── HelloWorldJNI.class
└── HelloWorldJNI.java
└── native.dll
The general set-up for running the code is:
java -cp . -Djava.library.path=/NATIVE_SHARED_LIB_FOLDER com.baeldung.jni.HelloWorldJNI
First of all, it took me some time to figure out the folder from which I had to call this in cmd, to not get the error
Error: could not find or load main class com.baeldung.jni.HelloWorldJNI
I solved this issue calling the previous line while being in the src/main/java directory. I have tried different options for the library path, but all returned
Exception in thread "main" java.lang.UnsatisfiedLinkError: no native in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at com.baeldung.jni.HelloWorldJNI.<clinit>(HelloWorldJNI.java:6)
Any suggestions on how to set the correct relative library path are more than welcome!
Thanks a lot in advance!