Reputation: 377
I am following this tutorial : http://stuf.ro/calling-c-code-from-java-using-jna . The difference is that my class is called main.c not ctest.c. I've created a library inside the project folder as it says there. On the next step i created the Java file with this line modified :
CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
to
CTest ctest = (CTest) Native.loadLibrary("main", CTest.class);
I have imported jna-4.1.0.jar to my project. On run it gives me this error :
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'main': Native library (win32-x86-64/main.dll) not found in resource path ([file:/D:/eclipse/workspace/RXTX/bin/, file:/D:/eclipse/workspace/RXTX/lib/RXTXcomm.jar, file:/C:/Users/Angelo/Downloads/jna-4.1.0.jar])
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
at com.sun.jna.Library$Handler.<init>(Library.java:147)
at com.sun.jna.Native.loadLibrary(Native.java:412)
at com.sun.jna.Native.loadLibrary(Native.java:391)
at HelloWorld.main(HelloWorld.java:9)
Where do i place my main.c and what do i do with the libctest.so file ?
Upvotes: 2
Views: 4570
Reputation: 22993
If you build a library libctest.so
then you need to load it with CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
(assuming the rest of your setup is correct).
Where to place the library is described in the javadoc of the class Native.
To get some hints you might print the steps of the JNA library search to the console.
System.setProperty("jna.debug_load", "true"));
CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
edit
How to call the class (amend the classpath and the directory name)
java -classpath jna.jar:. -Djna.library.path=/your/path/to/the/libctest/file HelloWorld
Upvotes: 2