简体   繁体   中英

java.lang.UnsatisfiedLinkError using jni

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)

将此添加到您对我有用的C编译器其他选项:

   -Wl,--export-all-symbols -Wl,--add-stdcall-alias

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM