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

Source: Internet
Author: User

1. Use JNI in JAVA to call C ++ code learning notes
Java-based JNI programming 1. What is JNI? JNI :( Java Natibe Inetrface. 2. Why Learning JNI? Java is a cross-platform language, but sometimes it still needs to call local code (these code is usually written by C/C ++ ). the JNI provided by Sun is a powerful interface of the Java platform. This JNI interface provides the function of calling each other between Java and the local code of the operating system. (Java calls C ++ code) the simplest Java calls C/C ++ code Step 1. Create a TestNativeCode project, create a cn. itcast package, and create a class TestNative. 2. Declare An native method in the java class. The TestNative. java code is as follows: copy the code package cn. itcast; public class TestNative {// use the JAVAH command to generate the C/C ++ header file containing the native method definition. // This Command requires JDK/bindirectory javah.exe public native void sayHello ();/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub} copy code 3. use the Javah command to generate the C/C ++ header file that contains the native method definition. compile and generate cn_itcast_TestNative.h. The specific method is as follows: Go to the cmd command prompt and execute the following command: cd E: \ Workspaces \ MyEclipse 8.5 \ TestNativeCode \ src javah cn. itcast. testNative4. open vs2008. Other languages -- compile all C ++ -- Win32 -- Win32 console application -- name: nativeCode -- OK.: 5. In the displayed dialog box, select application settings -- DLL -- empty project: 6. Move cn_itcast_TestNative.h to the new project directory. 7. select the header file directory -- Right-click to add existing items -- browse to add cn_itcast_TestNative.h8. select the source file -- Right-click to add -- new item -- C ++ file (. cpp) -- source. cpp -- add. source. cpp source code: copy the Code # include "cn_itcast_TestNative.h" # include <iostream> using namespace std; JNIEXPORT void JNICALL encode (JNIEnv * env, jobject obj) {cout <"Hello word! "<Endl;} copy Code 9. Copy jni. h and jni_md.h to the project, right-click the header file directory, and choose add existing item> Add. Note: jni. h In the Directory D: \ Program Files \ Java \ jdk1.7.0 _ 01 \ include, jni_md.h is in the Directory D: \ Program Files \ Java \ jdk1.7.0 _ 01 \ include \ win32 10. compile the C/C ++ source file into a dynamic link library (DLL) 11. add the DLL file to the path environment. In XP, right-click my computer and choose Properties> advanced> environment variable path: Add D: \ test \ nativeCode \ Debug 12. load the DLL in the Java class, and then call the native method of life. Modify the TestNative. java code as follows: copy the code package cn. itcast; public class TestNative {// use the JAVAH command to generate the C/C ++ header file containing the native method definition. // This Command requires JDK/bindirectory javah.exe public native void sayHello ();/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub System. loadLibrary ("nativeCode"); TestNative tst = new TestNative (); tst. sayHello () ;}} copy code 13. disable the myclipse8.5 software and open it again. Otherwise, an error is reported. Because myclipse reads the system variables every time it starts, it needs to be read again after modification, so it needs to restart the software. 14. Open the software again and you will see the Hello word code we wrote in 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.