Java is not perfect. In addition to its shortcomings, Java is much slower than traditional C ++ in terms of running speed. In addition, Java cannot directly access the underlying operating system (such as system hardware ), therefore, Java uses the native method to extend Java Program .
The native method can be compared to the interface of a Java program and a C program. The implementation steps are as follows:
1. Declare the native () method in Java and compile it;
2. Use javah to generate a. h file;
3. Write a. cpp file to implement the native export method, which must contain the. h file generated in step 2 (note that it contains the JNI. h file in JDK );
4. Compile the. cpp file in step 3 into a dynamic link library file;
5. Use the system. loadlibrary () method in Java to load the dynamic link library file generated in Step 4. This Native () method can be accessed in Java.
Java local method
1. To use a feature of the underlying host platform, this feature cannot be accessed through Java APIs.
2. to access an old system or use an existing library, this system or library is not written in Java.
3. In order to speed up the performance of the programCodeAs a local method.
First write the Java File
/*
* Created on 2005-12-19 author Shaoqi
*/
Package com. hode. hodeframework. modelupdate;
Public class checkfile
{
Public native void displayhelloworld ();
Static
{
System. loadlibrary ("test ");
}
Public static void main (string [] ARGs ){
New checkfile (). displayhelloworld ();
}
}
Then compiled into a class file based on the written file
Then, execute javah-JNI com. hode. hodeframework. modelupdate. checkfile in the class root directory such as classes or bin,
A com_hode_hodeframework_modelupdate_checkfile.h file is obtained in the root directory.
Compile the com_hode_hodeframework_modelupdate_checkfile.c File Based on the header file.
# Include "checkfile. H"
# Include
# Include
Jniexport void jnicall java_com_hode_hodeframework_modelupdate_checkfile_displayhelloworld (jnienv * ENV, jobject OBJ)
{
Printf ("Hello world! \ N ");
Return;
}
Compile and generate the DLL file, such as "test. dll". The name is the same as that in system. loadlibrary ("test ").
VC compilation method: CL-I % java_home % \ include \ Win32-LD com_hode_hodeframework_modelupdate_checkfile.c-fetest. dll
Add the parameter-djava. Library. Path = [DLL storage path] at runtime.
JNI entry:
This article describes how to use JNI technology to implement helloworld. The goal is to familiarize readers with the JNI mechanism and compile the first helloworld program.
Java Native Interface (JNI) is a local programming interface in Java and is part of j2sdk. In Java programs, we can use JNI to implement some functions that are inconvenient to implement in Java. JNI is usually used in the following situations.
- The standard Java class library does not provide the functions required by your application. Generally, these functions are platform-related.
- You want to use some existing class libraries or applications, but they are not written in Java.
- Some parts of the program have strict speed requirements. You can choose to compile orC LanguageTo implement and call them in Java
When I mention JNI in Java core technology, it is recommended that you do not have to use JNI technology unless you have more knowledge, on the one hand, if you use JNI, your program will lose portability. In this article, we skip the underlying mechanism of JNI. It is best for readers to think of it as an adhesive for local code and Java code. Shows the link:
Next, we will write the helloworld program. as it involves writing C/C ++ code, we will use Microsoft Vc ++ in development.
- Write Java code
We create a hello directory on the hard disk as our working directory. First, we need to write our own Java code. In the Java code, we will declare the native method, and the code is very simple. As shown below
Class helloworld
{
Public native void displayhelloworld ();
Static {
System. loadlibrary ("hello ");
}
Public static void main (string [] ARGs ){
New helloworld (). displayhelloworld ();
}
}
Note that our displayhelloworld () method declaration has a keyword native, indicating that this method is implemented in a language other than Java. The method does not include implementation, because we need to implement it in C/C ++ language. Note that system. the loadlibrary ("hello") code is defined in the static initialization block and is used by the system to load the hello shared library. This is the hello generated later. DLL. so)
-
- Compile Java code
Javac helloworld. Java generates the helloworld. Class File
- create. h file
in this step, we will use the javah command to generate. h file. This file will be used in the C/C ++ code later. Let's run
javah helloworld. In this way, we can see that a helloworld. h file is generated in the same directory. The content of the file is as follows
here we will not explain it too much.
/* Do not edit this file-it is machine generated */
# include
/* Header for class helloworld */
# ifndef _ included_helloworld
# DEFINE _ included_helloworld
# ifdef _ cplusplus
extern" C "{
# endif
/*
* class: helloworld
* method: displayhelloworld
* Signature: () v
*/
jniexport void jnicall java_helloworld_displayhelloworld
(jnienv *, jobject );
# ifdef _ cplusplus
}< br> # endif
- Compile local implementation code
In this section, we use the C/C ++ language to implement the methods defined in Java. We create a new project in VC ++ and then create a helloworldimp. cpp file. The content is as follows:
# Include <JNI. h>
# Include "helloworld. H"
# Include <stdio. h>Jniexport void jnicall
Java_helloworld_displayhelloworld (jnienv * ENV, jobject OBJ)
{
Printf ("Hello world! \ N ");
Return;
}
Note that we include JNI. h and the helloworld. h file we just obtained. Therefore, set JNI. h In java_home/include in VC ++. After compilation, generate the hello. dll file.
-
- Run Java program
Copy the hello. dll file generated above to our working directory. At this time, our Directory includes the helloworld. Java, helloworld. Class and hello. DLL files. Run the Java helloworld command, you can see hello World on the console |
.