[Java JNI] [Windows] [Visual Studio] [DLL] [Unsatisfiedlinkerror]

Source: Internet
Author: User

The study found that, whether using GCC or g++, gcc for Windows would modify our symbols when building DLL dynamic-link libraries, resulting in the JVM still not finding symbols even though the dynamic-link library was loaded. Thus generating java.lang.UnsatisfiedLinkError errors

So it's better to use Microsoft's own vs to generate DLLs, because the VS command line is complicated, and it's recommended to use a graphical interface, here are the configuration points.

(1) includes the Include and Include\win32 directories under the JDK directory for easy locating of jni.h and Jni_md.h

(2) If you use pure C code, include jni.h before inserting code #undef __cplusplus can make code hints more friendly.

Once configured, let's implement a Java native method.

Package Pkg;public class Winmessagebox {/** * pops up a global dialog box * @param label window title * @param content Form Contents * @return Call result */public Static native Boolean ShowMessageBox (String label, string content);}

Use Javah.exe to generate a header file that declares native methods that need to be implemented in the Winmessagebox class

Javah Pkg. Winmessagebox

Get the file pkg_winmessagebox.h and use vs to implement it.

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Pkg_winmessagebox */#ifndef _included_pkg_winmessagebox#define _included_pkg_winmessagebox#ifdef __cplusplusextern "C" {#endif/* * Class:     Pkg_winmessagebox * Method:    showmessagebox * Signature: (ljava/lang/string; ljava/lang/string;) V */jniexport Jboolean jnicall java_pkg_winmessagebox_showmessagebox  (JNIENV *, Jclass, Jstring, jstring); #ifdef __cplusplus} #endif #endif

  

#undef __cplusplus#include "H:\Code\EclipseWorkSpace\TEST\bin\pkg_WinMessageBox.h" #include <windows.h># Include <stdlib.h> #include <stdio.h> #include <time.h>jniexport jboolean jnicall java_pkg_ Winmessagebox_showmessagebox (jnienv *env, Jclass clazz, jstring label, jstring content) {MessageBoxW (HWND) NULL, ( LPCWSTR) (**env). Getstringchars (env, content, 0), (LPCWSTR) (**env). Getstringchars (env, label, 0), MB_OK), Srand ((UINT) time (0)), return rand ()% 2 > 0? True:false;}

After you build the project, write the test

In this process we find that although we need to use the functions in the dynamic link library JVM.dll, we do not need to explicitly link it, This is because our call to the JVM function is called by a function pointer table in the struct object pointed to by the struct object pointer that is passed in to the jnienv pointer in the formal parameter, and the Java Runtime packs the JVM context for us and fills out the table, passing it as a parameter when calling the local method. This allows us to invoke the function in the local method using the pointer in the table.

[Java JNI] [Windows] [Visual Studio] [DLL] [Unsatisfiedlinkerror]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.