Use JNI to call C + + code learning notes in 1.JAVA

Source: Internet
Author: User
Tags stub

                                   Java JNI programming
1. What is JNI?

   JNI: (Java natibe inetrface) abbreviation.

2. Why study JNI?
 java is a cross-platform language, but at some point it is still necessary to invoke local code


The simplest Java call C + + code step

1. Create Testnativecode project, new Cn.itcast package, new Class class testnative.

2. Declare a native method in the Java class.

The Testnative.java code is as follows:

 PackageCn.itcast; Public classtestnative {//Use the Javah command to generate a C + + header file that contains the native method definition. //the command needs to use the Jdk/bin directory under Javah.exe     Public native voidSayHello (); /**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub    }}

3. Use the Javah command to generate a C + + header file that contains the native method definition.

Compile build cn_itcast_testnative.h

Here's how:

go to the cmd command prompt to execute The following command:

CD E: CD   8.5\testnativecode\srcjavahcn.itcast.TestNative 

4. Open vs2008, Other languages--visuall c++--win32--win32 console Application--Name: Nativecode --OK.



5. In the popup dialog box, select Application Settings--dll--Empty Project

6. Move the cn_itcast_testnative.h to the new project directory.

7. Select the header file directory--right--Add Existing Item--Browse to add cn_itcast_testnative.h
8. Select the source file--right-click Add-New Item--c++ file (. cpp)--source.cpp--Add.

Source.cpp source code is as follows:

#include"cn_itcast_testnative.h"#include<iostream>using  namespacevoid jnicall Java_cn_itcast_testnative_sayhello (jnienv * env, Jobject obj) {    cout<<"Hello word! "<<Endl;}

9. Copy jni.h, Jni_md.h to project, select header file directory-right-Add Existing Item-add these two.

Attention:

jni.h in the D:\Program files\java\jdk1.7.0_01\include directory,

jni_md.h under the D:\Program files\java\jdk1.7.0_01\include\win32 directory

10. Compiling a C + + source file into a dynamic-link library (DLL)

11. Add DLL files to the path environment, under XP My Computer--right-click Properties-Advanced-environment variables

Path: Add D:\test\nativeCode\Debug

Load the DLL in the 12.Java class, and then call the native method of life.

The Testnative.java code is modified as follows:

 PackageCn.itcast; Public classtestnative {//Use the Javah command to generate a C + + header file that contains the native method definition. //the command needs to use the Jdk/bin directory under Javah.exe     Public native voidSayHello (); /**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.loadlibrary ("Nativecode"); Testnative TST=Newtestnative ();    Tst.sayhello (); }}

13. Turn off the myclipse8.5 software and turn it back on, or you will get an error.

Because Myclipse will read the system variable every time it starts, it needs to be re-read after it has been modified, so restart the software.

14. Turn the software on again to see that the Hello Word code we wrote in C + + is called.

Use JNI to call C + + code learning notes in 1.JAVA

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.