Jni basics (I): Java program calls C/C ++

Source: Internet
Author: User
ArticleDirectory
    • The simplest steps for Java to call C/C ++ code

JNI (Java Native Interface)

Java is a cross-platform language, but sometimes it still needs to be called locallyCode(These codes are usually written by C/C ++)

The JNI provided by Sun is a powerful interface of the Java platform. This JNI interface providesFunctions of mutual calls between Java and Local Operating System Code.

 

The simplest steps for Java to call C/C ++ code

1. First declare a native method in the Java class:

 
Public ClassTestnative {
Public Native VoidSayhello ();
......
}

2. Use the javah command to generate the C/calong header file containing the native method definition (find javah.exe in the bindirectory of JDK): javah com. XX. testnative (full package name class file)

My class file is in D: \ Users \ Administrator \ workspaces \ myeclipse \ testnative \ bin. Therefore, run javah com. Jim. Test. testnative from the command to this folder.

Get com_jim_test_testnative.h, a C ++ header file:

 1   /*  Do not edit this file-it is machine generated  */ 
2 # Include " JNI. h "
3 /* Header for class com_jim_test_testnative */
4
5 # Ifndef _ included_com_jim_test_testnative
6 # Define _ Included_com_jim_test_testnative
7 # Ifdef _ cplusplus
8 Extern " C " {
9 # Endif
10 /*
11 * Class: com_jim_test_testnative
12 * Method: sayhello
13 * Signature: () V
14 */
15 Jniexport Void Jnicall java_com_jim_test_testnative_sayhello
16 (Jnienv *, jobject );
17
18 # Ifdef _ cplusplus
19 }
20 # Endif
21 # Endif

3. Write the C/C ++ source file according to the generated C/C ++ header file.

Use Visual Studio to create a DLL ConsoleProgramAnd write the source file source. cpp according to the methods in the header file:

1# Include <iostream>
2# Include"Com_jim_test_testnative.h"
3 Using NamespaceSTD;
4
5JniexportVoidJnicall java_com_jim_test_testnative_sayhello (jnienv * ENV, jobject OBJ ){
6Cout <"Hello world!"<Endl;
7}

This program is displayed and only prints Hello world!

4. Compile the C ++ source file into a dynamic link library (DLL)

But at this time, the compilation will certainly fail, because the second line in com_jim_test_testnative.h is still introduced.JNI. hHeader file, where is this header file,Under the include directory under JDKBut before the end, this header file is introducedJni_md.hSo we need to introduceInclude/Win32 jni_md.hWell, all of them have been introduced.-But there are still some details about # include <XXX. h> changed to # include "XXX. H "from the standard library path to the current directory, otherwise the program still cannot compile and report an error.

5. Add the DLL file to the Java program.

After successful compilation, we are not far from the victory. Next we need to put the compiled DLL file under classpath. One way is to set the environment variable, the other method is to copy the DLL file directly. Which one do you like anyway? I copy the DLL file directly to the project directory of the Java program, and then execute the Java program:

 1   Public   Class Testnative {
2 Public Native Void Sayhello ();
3 Public Static Void Main (string [] ARGs ){
4 System. loadlibrary ("testnative "); // Use loadlibrary to load DLL files
5 Testnative Tn = New Testnative ();
6 Tn. sayhello ();
7 }
8 }

If no exception occurs, the message "Hello World!" is displayed successfully!

 

In case of an accident, it is hard to say that the codeblocks I tried for the first time is tragic, because it comes with the GCC/g ++ compiler, the compiled DLL file cannot be normally called by JNI. It seems that additional commands need to be added during compilation. You can find it online. I tried it too much, directly change Visual Studio. It is no problem to use.

 

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.