JNI Learning Series -- Calling C/C ++ in Java

Source: Internet
Author: User
Call C/C ++ in JavaFor a preliminary understanding of JNI, please refer to the previous JNI Learning Series -- a preliminary understanding of JNI. This section describes how to compile a simple Java program to call C \ c ++;

1. Compile the Java class testnative. Java with native Declaration

Package COM. yin. JNI; public class testnative {// native keyword declares the localization method. You do not need to use Java code to implement public native void sayhello (); public static void main (string [] ARGs) {}}

2. Go to the process directory where testnative. Java is located and run javac testnative. Java to compile and generate the. Class file. Run javah testnative to generate a header file com_yin_jni_testnative.h,
The file name is composed of package name + JNI + class name;

Com_yin_jni_testnative.h
/* DO NOT EDIT THIS FILE - it is machine generated */#include "jni.h"/* Header for class com_yin_jni_TestNative */#ifndef _Included_com_yin_jni_TestNative#define _Included_com_yin_jni_TestNative#ifdef __cplusplusextern "C" {#endif/* * Class:     com_yin_jni_TestNative * Method:    sayHello * Signature: ()V */JNIEXPORT void JNICALL Java_com_yin_jni_TestNative_sayHello (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

Java_com_yin_jni_testnative_sayhello (jnienv *, jobject) is the c ++ method generated for sayhello in Java after compilation. It consists of Java + package name + JNI + class name + method name;

3. Create a new C ++ project. I use VC ++ 6.0 to create a new project --> Win32 dynamic link libnary --> empty. dll project; Project name: nativecode
Import com_yin_jni_testnative.h to head files of the project and create a source in source files. CPP file. Copy the jniexport void jnicall java_com_yin_jni_testnative_sayhello (jnienv *, jobject) code to source. CPP; Add the parameter (jnienv *, jobject) to (jnienv * ENV, jobject OBJ ).

4. after compiling the C ++ project, the error "can't find" JNI. h "cannot find JNI. h. copy the H file to the project directory, JNI. h. In the include directory of the Java JDK,
Copy jni_md.h in Win32 together, because JNI. H will call this file;
Note the introduction of JNI. # include <JNI. h> An error may be reported and changed to "JNI. H ", because # include <JNI. h> the compiler will find it in include under the system environment variable, so that it will not be found.
This header file is already in use;

5. After compilation is successful, an nativecode. dll file will be generated under the C ++ project directory so that the. dll file can be called in Java code;
Note: You must set. add the directory where the DLL file is located to the environment variable. Otherwise, the directory cannot be found during program running. DLL file. At the same time, restart eclipse because the environment variable of the current system has been loaded at eclipse startup. After modifying the environment variable, eclipse cannot find the newly added environment variable.

6. Call the generated. dll file in Java.

Package COM. yin. JNI; public class testnative {// native keyword declares the localization method. You do not need to use Java code to implement public native void sayhello (); public static void main (string [] ARGs) {// load the generated nativecode. DLL file system. loadlibrary ("nativecode"); testnative Tn = new testnative (); // call the sayhello method tn. sayhello ();}}

Run the Java program. The console prints hello, my first JNI program, which means you have succeeded.

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.