Java uses JNI to invoke VC + + generated DLLs (Java interacts with C + +)

Source: Internet
Author: User

In response to project needs, Android calls Java,java and then calls C + + to implement an image matching feature on Android, and we need to interact with C + + and Android as a Java group. Here's how Java and C + + use JNI to interface, I do a demo and execute successfully.

What is JNI?

JNI full spell is Java Native Interface, Chinese meaning for Java local calls.

The JNI standard is part of the Java platform, which agrees that Java interacts with code in other languages. JNI starts with a locally compiled language. Especially in C and C + +, we are also able to communicate using JNI and other languages. We just have to call the Convention to be supported.

What's the principle?

we know that. NET, such as C + + or C #, can generate a. dll class library, while C + + (or. net) The. dll file generated in the Java program is equivalent to the. class file generated by the Java source code in the Java application, and we can call the C + + DLL file directly through code in a JavaScript program.

in the C language, it is assumed that a dynamic link library DLL file is generated. Two parts are required, part of the header file for. h, and this file is responsible for the declaration of some methods. The. cpp file is responsible for the implementation of the method. In Java, there is a jni.h and jni_md.h in the include package that installs the JDK folder, which can be said to be an interface published by Java, which can be used directly in C + + to compile the C + + method to be called by Java, which is called by Java. And be able to receive the number of references that Java has passed over. directly on the code.


detailed procedures such as those seen below (Java publishes an interface that enables C + + to multiply the functionality) 1, the establishment of Java source code such as the following see.

build Java project and build the COM package under SRC. Build our Java class Reliability.java below the COM package, as seen in the following code.

Package Com;public class Reliability {static {try {system.loadlibrary ("reliability"),//Call DLL} catch (Unsatisfiedlink Error e) {System.out.println ("The Load Problem");}} public native int Shanfei (int i);p ublic static void Main (string[] args) {Reliability reliability = new reliability (); System.out.print (Reliability.shanfei (6));}}

2. Compile the. Java source file.

we want to implement the result of passing int integer parameters to achieve our own multiplication, and the implementation of the Shanfei (int p) method is C + +. We are able to load a C + + generated DLL through the system.loadlibrary ("Reliability") method. The method interface and the parameters are declared through native.      

After you have written your own Reliability.java code, you can use MyEclipse to compile to a Reliability.class file or to compile the. class file directly with the Javac command.

My use of the MyEclipse is compiled, and the Reliability.class file is generated in the/reliability/bin/com directory.


3. Use the Javah command to let the Reliability.class file generate the. h file required by C + +. That is, the interface to C + +.

It is important to note that you must go to the top-level folder you compiled into a class file for the Javah command. And your JDK is already configured with environment variables. The environment variable here is java_home: Install the upper folder of the JDK Bin folder, for example my JDK is installed in C:\java\bin ..., then my java_home is:C:\java . Path folder: Refer to Java_home and add \ bin to %java_home%\bin .

Classpath for: .; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar; This is sure to be configured well. Then write the following command in a DOS form such as the following. The file generated to C + +. H.



this will cause the. h file to appear in your class file in the first-level folder and into your myeclipse. Sibling to COM package.


4, so Java has been published to C + + com_reliability.h header file. C + + is going to implement this approach.

because we want to build DLL files, we need to create a dynamic link library file, assuming that the C + + 6.0 environment, is to establish the Projects--dynamic-link Library project.

After adding our Com_reliability.h file to our DLL project, the folder structure is seen in the following example.


The code in the header file, as seen below, is com_reliability.h.

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class com_reliability */#ifndef _ Included_com_reliability#define _included_com_reliability#ifdef __cplusplusextern "C" {#endif/* * Class:     com_ Reliability * Method:    Shanfei * Signature: (i) I */jniexport jint jnicall java_com_reliability_shanfei  (jnienv *, Jclass, jint); #ifdef __cplusplus} #endif #endif

     The implementation of the Reliability.cpp code is seen in the following example.

#include <jni.h> #include "com_reliability.h" #include <stdio.h> #include <jni_md.h>jniexport jint Jnicall Java_com_reliability_shanfei (jnienv *, Jclass, jint p) {int j = p*p; return j;}

Avoid :. cpp and the method names in. h must be the same!!

and paste the jni.h and Jni_md.h found from Java into an external dependent directory, or directly into the Vc98\include directory.


5. Generating DLL files

so we ctrl+f7 compile, F7 in C + + The project debug generates the Reliability.dll file so that we can put the DLL file into the Java compiled. Class folder at the same time as the MyEclipse and SRC peers. To execute Java's Reliability.java file like this, you see the results of C + + giving us a calculation.

The demo sample is shown below for example.



Summary:

This is roughly the process. In this process will appear such or that the problem, I also constantly try to make mistakes, try to modify, the most fundamental is the computer error. Must be oneself which place has the mistake, holds this mentality. Patiently find the answer, try, think, set breakpoints debugging. To see exactly what step is wrong, finally will solve this problem, let you excited to see the results of the implementation.





Java uses JNI to invoke VC + + generated DLLs (Java interacts with C + +)

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.