MINGW-GCC how to compile a JNI program

Source: Internet
Author: User
Tags naming convention win32

The first step: writing Java programs
public class HelloWorld {
public native void Displayhelloworld ();

static {
System.loadlibrary ("Hello");
}

public static void Main (string[] args) throws Exception {
New HelloWorld (). Displayhelloworld ();
}
}
Notice where the Code
public native void Displayhelloworld ();
is to declare that Displayhelloworld () is a local method that needs to be implemented in JNI.
static {
System.loadlibrary ("Hello");
}
It means that the download of the file in the library implies that our following JNI program eventually needs to be packaged into Hello.dll

Step two: Compile the Java program
Javac Helloworld.java

Step three: Generate header Files
Javah-jni HelloWorld

Step Fourth: Write local implementation code
We open the third step generated by the HelloWorld.h this file, find the method declaration
jniexport void Jnicall Java_helloworld_displayhelloworld
(JNIENV *, jobject);
This is the JNI naming convention, specifically
You can refer to the Java tutorial. Here's just the method declaration, and now we're going to implement it.
* HELLOWORLDIMP.C * *
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>

Jniexport void Jnicall
Java_helloworld_displayhelloworld (jnienv *env, Jobject obj)
{
printf ("Hello world!\n");
Return
}

Step Fifth: Create a library file
As mentioned in the first step, Hello.dll is generated using the previously mentioned command to make a dynamic library.
Gcc-i%java_home%\include-i%java_home%\include\win32-shared-o Hello.dll helloworldimp.c
Run
Java HelloWorld
No, it's wrong.
Exception in thread "main" Java.lang.UnsatisfiedLinkError:displayHelloWorld
At Helloworld.displayhelloworld (Native method)
At Helloworld.main (helloworld.java:9)
This means that the library file has been successfully loaded, but no matching displayhelloworld is found. But we have realized this method. The original program in the call to the dynamic library, not as simple as we imagined, and different compiler practices, Windows version Java invoke JNI to follow the way the VC calls, and we use the MinGW GCC default format inconsistent. We need to adjust the parameters (note the--kill-at)


gcc-i%java_home%\include-i%java_home%\include\win32-shared-wl,--kill-at-o hello.dll helloworldimp.c


Run


Java HelloWorld


Hello world! Run successfully, great

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.