I. Usage
When you cannot write the entire application in Java, JNI allows you to use the local code.
In the following typical cases, you may decide to use the local code:
1. I hope to use a lower-level and faster programming language to implement code with strict time requirements.
2. You want to access the old code or code library from a Java program.
3. Platform-dependent features that are not supported in standard Java class libraries are required.
II,Required Software
Eclipse3.4.1,JDK 6 and vc6.0
3. Step Analysis
The process of Calling C or C ++ code from a Java program consists of six steps:
We will discuss each step in depth on the following pages, but let's take a quick look at them:
1. Write Java code.
We will start with writing Java classes that execute three tasks:
1) Declare the local method to be called;
2) load the shared library containing the local code;
3) then call the local method.
2. Compile the Java code.
Before using Java classes, you must compile them into bytecode.
3. Create a C/C ++ header file.
The C/C ++ header file declares the description of the local function to be called.
Then, this header file is implemented with the C/C ++ function (see step 4) to create a shared library (see step 5 ).
4. Write C/C ++ code.
This step implements functions in the C or C ++ source code file.
The C/C ++ source file must contain the header file created in step 3.
5. Create a shared library file.
Create a shared library file from the C source code file created in step 4.
6. Run the Java program.
Run the code and check whether it is useful. We will also discuss some tips for solving common errors.