java.lang.UnsatisfiedLinkError - JNI

29,336

Solution 1

I think the problem is that your JNI signature doesn't match. That is:

JNIEXPORT void JNICALL native_MessageBox(string text, string title);

should be something like:

JNIEXPORT void JNICALL java_com_example_Wrapper_native_MessageBox(string text, string title);

where, java_com_example should be replaced with your package name (. to be replace with _ in package name).

OR

I would suggest you to generate your native function signature and declaration using javah -jni option available in java.

Solution 2

If you test with

nativeFile = new File(filename + ".dll");
    if (!nativeFile.exists())
        System.exit(1);

you should use it !!

System.load(nativeFile);

There are two different ways to load a native library into a running Java program:

  • System.loadLibrary(String) and System.load(String).

  • The System.loadLibrary method allows us to load a library from the "default" path.

    System.loadLibrary("HelloWorld");

  • System.load allows us to load a library from anywhere via its absolute path.
    System.load("c:/path/to/dll/HelloWorld.dll");

Share:
29,336

Related videos on Youtube

user2348979
Author by

user2348979

Updated on April 30, 2020

Comments

  • user2348979
    user2348979 about 4 years

    I keep getting a java.lang.UnsatisfiedLinkError error every time I run my program. I have a native, a wrapper, and the program to call the native through the wrapper.

    main.h

    #ifndef __MAIN_H__
    #define __MAIN_H__
    
    #include <windows.h>
    #ifdef BUILD_DLL
        #define DLL_EXPORT __declspec(dllexport)
    #else
        #define DLL_EXPORT __declspec(dllimport)
    #endif
    
    #include<jni.h>
    #include<iostream>
    
    using namespace std;
    
    extern "C"
    {
    
    JNIEXPORT void JNICALL native_MessageBox(string text, string title);
    
    }
    
    #endif
    

    main.cpp

    #include "main.h"
    
    #include<windows.h>
    #include<iostream>
    
    using namespace std;
    
    JNIEXPORT void JNICALL MsgBox(string text, string title)
    {
        MessageBox(NULL, text.c_str(), title.c_str(), MB_OK);
    }
    
    extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
    LPVOID lpvReserved)
    {
        switch (fdwReason)
        {
            case DLL_PROCESS_ATTACH:
                break;
    
            case DLL_PROCESS_DETACH:
                break;
    
            case DLL_THREAD_ATTACH:
                break;
    
            case DLL_THREAD_DETACH:
                break;
        }
        return TRUE;
    }
    

    Wrapper.java

    public class Wrapper
    {
       private static File nativeFile = null;
    
           static
       {
                //ArchitectureReader and OSReader are classes I made to read the CPU              
                //bits and the OS
        ArchitectureType archType = ArchitectureReader.getArcitecture();
        OSType osType = OSReader.getOS();
    
        String filename = "C:/native-";
    
        if (osType == OSType.Windows)
        {
            if (archType == ArchitectureType.BITS_32)
                filename += "win32";
            else if (archType == ArchitectureType.BITS_64)
                filename += "win64";
            else
                System.exit(1);
        }
        else
            System.exit(1);
    
        nativeFile = new File(filename + ".dll");
        if (!nativeFile.exists())
            System.exit(1);
    
        System.load(filename); //This is where the Exception is thrown
    }
    
    private static native void native_MessageBox(String text, String title);
    public static void MesageBox(String text, String title)
    {
        native_MessageBox(text, title);
    }
    
    public static String getNativePath()
    {
        return nativeFile.getAbsolutePath();
    }
    }
    

    Main.Java

    public class Main
    {
    public static void main(String[] args)
    {
        Wrapper.MessageBox("Testing JNI", "JNI");
    }
    }
    

    The native was built using MinGW 64 bit. Anyway, I don't get why I get the error. Help?????

  • Micho Rizo
    Micho Rizo over 9 years
    Yep, i just had a similar problem. Was trying to use a lib from another jni project but kept getting this UnsatisfiedLinkError since I was using a different package name than the original project. Used the original package name in the new project and problem resolved.