1
class HelloWorld {
    public native void print();  //native method
    static   //static initializer code
    {
        try{
            String path = System.getProperty("java.library.path");
            System.out.println(path);
        System.loadLibrary("CLibHelloWorld1");
        //System.load("C:/TE_CDA_Project/Test/native/CLibHelloWorld1.dll");
        //Runtime.getRuntime().load("C:/TE_CDA_Project/Test/native/CLibHelloWorld1.dll");

    System.out.println("Loaded CallApi");
    }catch(UnsatisfiedLinkError e){
        e.printStackTrace();
    }
}

public static void main(String[] args)
{
    HelloWorld hw = new HelloWorld();

    hw.print();


}

}

Output:

C:\TE_CDA_Project\Test\native
Loaded CallApi
Exception in thread "main" java.lang.UnsatisfiedLinkError: Graph.HelloWorld.print()V
    at Graph.HelloWorld.print(Native Method)
    at Graph.HelloWorld.main(HelloWorld.java:26)
Vignesh Vino
  • 1,242
  • 4
  • 25
  • 50
Paritosh
  • 11
  • 1
  • 2
  • This is most probably because the Path variable for java.library.path isn't currently set. This is the path from where Java extracts required libraries. – Vignesh Vino May 14 '13 at 09:53
  • @Paritosh: I think you should post your native code also. This exception normally occurs due to wrong signature of the native method. See the example here: http://stackoverflow.com/questions/16518490/java-lang-unsatisfiedlinkerror-jni/16519301#16519301 (if you are facing the same problem, dont forget to +1 that answer) – Zax May 15 '13 at 04:25

3 Answers3

1

add this to your C Compiler additional options it worked for me:

   -Wl,--export-all-symbols -Wl,--add-stdcall-alias
Tarik FAMIL
  • 439
  • 5
  • 9
0

According to this error

Exception in thread "main" java.lang.UnsatisfiedLinkError: Graph.HelloWorld.print()V

I can assume you specified the path to native library correctly, this problem probably means Java cannot map the native method to actual native function. So there should be some problem with the native method in the library, probably the method signature which you are trying to access may be wrong.

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33
0

That the error is saying is that you have attempted to call a method like this:

void native print();

declared in Graph.HelloWorld, but the JVM has not been able to find the native code implementation for that method.

This could be caused by a couple of things:

  • You application has not made the System.loadLibrary(...) to load the native library.

  • The native library that you loaded doesn't declare a method that matches the name and signature of the method on the Graph.HelloWorld class. (The classname could be wrong, or the method name, or the argument types or result types.)


While I have your attention, Graph.HelloWorld is a serious Java style violation. Assuming that Graph is a package name, it should be entirely lower case. Furthermore, it is advisable (for anything other than "throw away" code) to follow the convention of prefixing the package name with reversed domain name ... so that your "graph" package doesn't accidentally collide with someone else's.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216