JNI Notes 1, jni notes

Source: Internet
Author: User

JNI Notes 1, jni notes
1. What is JNI?

Java Native Interface (JNI) is a local programming Interface in Java.

It is a function interface for Java and the local operating system code to call each other.

2. Steps for Java to call C/C ++:

 

1. Declare the native method in the Java class

Create a Java project TestNativeCode package com. fomagic class Testnative

1 package com. fomagic; 2 3 public class TestNative {4 public native void sayHello (); // The local code of C ++ implements 5 public static void main (String [] args) {6 7} 8}
2. Use the javah command to generate the C/C ++ header file containing the native method definition

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

1) create a Win32 console application in VS-> OK-> next (select application settings)

2) copy the three files to the project directory.

Header file generated in step 3 of com_fomagic_TestNative.h

Jni. h is located in the include file directory under JDK.

Jni_md.h include-> win32 Directory

3) Add the existing item com_fomagic_TestNative.h to introduce the header file ()

4) Add the following content to the source file source. cpp:

5) Note that the referenced header file "<>" indicates that the system and "indicate that local queries are preferred.

 

 

1 # include "inline" 2 # include <iostream>; 3 using namespace std; 4 5 // define method declared in com_fomagic_TestNative.h 6 JNIEXPORT void JNICALL inline (JNIEnv * evn, jobject obj) 7 {8 cout <"Hello World! "<Endl; 9}4. Compile the C/C ++ source file into a dynamic link library (DLL) and execute binary code.

Run the build solution to generate the DLL file in Debug under the project directory.

 

5. Add the PATH of the DLL file to the PATH environment variable.

This configuration of Java environment variables should be clear, so that Java programs can directly call the current dynamic link library

Note: You need to restart Eclipse to re-load environment variables to develop Java programs.

6. Load the DLL in the java class, and then call the declared native method.
1 package com. fomagic; 2 public class TestNative {3 public native void sayHello (); // C ++ local code implementation 4 public static void main (String [] args) {5 System. loadLibrary ("NativeCode"); // load the dynamic link library, which cannot be added. dll 6 TestNative test = new TestNative (); 7 test. sayHello (); 8} 9}
7. Run the Java Project

 

1) execution result: Hello World!

2) exception information: Can't load IA 32-bit. dll on a AMD 64-bit platform

Solution:

When the Java referenced DLL Dynamic Link Library mentioned above, the execution result exception information is: Can't load IA 32-bit. dll on a AMD 64-bit platform

Cause: Because your windows operating system is 64-bit, the win32 console program generates a 32-bit DLL dynamic link library by default.

Solution: we can imagine that we need a 64-bit DLL dynamic link library to generate

1. Configure through the original VS Project ()

2. At this time, NativeCode. dll is generated in the project directory --> x64 --> Debug directory.

3. Similarly, add the PATH of the DLL file to the PATH environment variable to facilitate Java program loading. Eclipse needs to restart and reload the environment variable.

 

Iii. Disadvantages and points of attention

 

 

 

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.