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. It agrees with the code in Java and other languages to interact. JNI begins with a native compiled language, especially C and C + +. We can also use JNI to communicate with 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. The. dll file generated in C + + (or. net) is equivalent to the. class file generated by the Java source code in our Java program. We can call C + + DLL files directly through code in a Java 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, this file is responsible for the declaration of some methods, and 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 a Java-advertised interface that can be compiled directly from the two head-type files in C + + for the C + + method to be invoked by Java. Let Java call and be able to receive the number of references that Java passes 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, 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 C + + generated DLLs through the system.loadlibrary ("reliability") method, declaring the method interface and the parameters 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 it is important to go to the top-level folder you compiled into a class file for the Javah command, and your JDK has been 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 the level folder on your class file. and introduced into your myeclipse, with the COM package peers.


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

Since we are building DLL files, we need to create a dynamic link library file. Assuming 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 is seen in the following example. 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 the external dependent directory. Or put it directly in the Vc98\include directory.


5. Generating DLL files

so we ctrl+f7 compile, F7 in the C + + project debug to generate Reliability.dll file, so we put this DLL file in Java compiled. Class folder. At the same time put the MyEclipse and SRC peers. This executes the Java Reliability.java file. just see C + + give us the results of the 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 the wrong place, try to modify, the most fundamental is the computer error. Must be oneself which place has the mistake, holds this mentality, patiently looks for the answer, the attempt, the Ponder, sets the breakpoint debugging, to see exactly which step appears the mistake, finally will solve this question, lets you be excited to see the execution result.





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.