0
votes

I am trying to run cpp code in java. but getting this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no math in java.library.path

I have tried to link my library to the java.library.path using this command after

$ javac HelloJNI.java

$ java -Djava.library.path="/home/sneha/Videos/node-ffi-example/math.so" HelloJNI

But still the same error.

Here is my HelloJNI.java file

public class HelloJNI {  // Save as HelloJNI.java
   static{

      System.loadLibrary("math"); // Load native library hello.dll (Windows) or libhello.so (Unixes)
                                   //  at runtime
                                   // This library contains a native method called sayHello()
      System.out.println(".so included");
   }

   // Declare an instance native method sayHello() which receives no parameter and returns void
   private native int num();

   // Test Driver
   public static void main(String[] args) {
     HelloJNI hj = new HelloJNI();
     int num = hj.num();  // Create an instance and invoke the native method
     System.out.println("Number: "+num);
   }
}

Please help, Thanks !

2

2 Answers

2
votes

You have to make sure to to use directory location:

-Djava.library.path="/home/sneha/Videos/node-ffi-example"

instead of

-Djava.library.path="/home/sneha/Videos/node-ffi-example/math.so"

Also, try using some sample that works out of the box to get familiar with JNI. For example: http://jnicookbook.owsiak.org/recipe-No-001/ or go directly to the sources: https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo001

0
votes

try using a .dll instead.(that worked for me)