JNI Java calls the C + + interface

Source: Internet
Author: User

1, the definition of JNI (from Baidu Encyclopedia)

JNI is the abbreviation for Java Native interface, and Chinese is a native call to Java. Starting with Java1.1, the Java Native Interface (JNI) standard becomes part of the Java platform, which allows Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other languages, as long as the calling convention is supported.

2. Java calls C + + interface steps via JNI

1), creating Java packages, classes

Create Package Myjni here, class Testjni

 Package Myjni;  Public class testjni {    }

2), create the native method in the Testjni class

Create SayHello () method here

 Package Myjni;  Public class Testjni {    publicnativevoid  SayHello (); }

3), using the Javah command to generate C + + needs to use the header file

Here is the directory structure of my Java project:

|-eclipse_workspace

|-testjni

|-src

|-myjni

|-testjni.java

--Use the command line to enter the directory where the Java project is located, and go to a directory similar to SRC

      

--Generate the. h file with the Javah command, because Testjni.java is in the package Myjni, at this point the command is executed in the SRC directory, so add the package Myjni

      

After the--javah command executes successfully, the. h file is generated in the directory where the command was executed (src directory here)

      

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Myjni_testjni*/#ifndef _included_myjni_testjni#define_included_myjni_testjni#ifdef __cplusplusextern "C" {#endif/** Class:myjni_testjni * Method:sayhello * Signature: () V*/Jniexportvoidjnicall Java_myjni_testjni_sayhello (jnienv*, jobject); #ifdef __cplusplus}#endif#endif

4), create C + + Engineering

--New project

    

--Application Type set to DLL type

    

--Copy the previously generated. h file to the C + + project and put it in the same directory as the. cpp

    

--Copy the Jawt_md.h and Jni_md.h under the jni.h and Include/win32 directories in the JDK installation directory include directory to the Vc/include directory under the VS installation directory

--Copy the DLL files generated in the C + + project to the directory contained in the environment variable path, and you can view the directories contained in the path in the environment variable

Mine is to copy the TestJNI.dll to D:\Program Files\java\jdk1.7.0_17\bin.

--Writing the SayHello () method in C + + engineering

The method name must match the method name in TestJNI.h, otherwise an error occurs

#include <iostream>#include"myjni_testjni.h"void Jnicall Java_myjni_testjni_sayhello (jnienv *env, Jobject obj) {      usingnamespace  STD;      cout<<"Hello world! "<<Endl;}

5), write Java program, call C + + SayHello () interface in Java

--system.loadlibrary ("Testjni") is the load DLL file, the file name must be the DLL file name generated by the C + + project;

--testjni.sayhello () and interfaces that invoke C + + implementation;

 Package Myjni;  Public class Testjni {    publicnativevoid  SayHello ();      Public Static void Main (string[] args) {        system.loadlibrary ("Testjni");         New Testjni ();        Testjni.sayhello ();    }}

6), run Java program, print the result is Hello world! Indicates that the C + + interface was successfully invoked through JNI.

    

JNI Java calls the C + + interface

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.