Introduction to JNI
JNI is the abbreviation of Java Native Interface, which provides several APIs to implement communication between Java and other languages (mainly C&C++). Starting from Java 1.1, the JNI standard has become part of the java platform, which allows Java code to interact with code written in other languages. JNI was initially designed for native compiled languages, especially C and C++, but it doesn't prevent you from using other programming languages, as long as the call convention is supported. Using java to interact with locally compiled code often loses platform portability. However, in some cases, this is acceptable, or even necessary. For example, use some old libraries to interact with hardware, operating systems, or to improve program performance. The JNI standard must at least ensure that local code can work in any Java virtual machine environment. Simply put, JNI is convenient for Java to call functions in C and C++ languages; Java provides interfaces (header files), c and C++ to implement these functions for Java to call.
There is no interface for clearing the screen in Java, and all system("cls") in the C language can be called through jni. Of course, it is limited to cmd screen clearing, and does not support eclipse console console clearing.
step:
1. Write Java's screen clearing native interface.
public class Clear { static{ System.loadLibrary("clear");//Load clear.dll dynamic library} public native static void clsCmd();//Clear screen}2. Compile Clear.java and generate Clear.class
3. Use javah to generate the corresponding C language header file Clear.h
4. Use Visual Studio to create win32 projects and generate corresponding dll dynamic libraries
4.1 Create a project
4.2 Application Type: DLL
4.3 Import the header file Clear.h generated in step 3 and the two header files jni.h and jni_md.h that need to be relied on to the project
jni.h is in the include directory of jdk installation path.
jni_md.h is in the win32 directory in the include directory of the jdk installation path.
Copy and paste these three header files into the project directory:
Then import them in VS:
Header file --->Add --->Existing items --->Select the three header files above --->Add
Change #include <jni.h> in the Clear.h header file to #include "jni.h"
4.4 Add .cpp file and write c code.
Source File --->Add --->New Item --->C++ File:clear.cpp
#include "Clear.h"#include <iostream>JNIEXPORT void JNICALL Java_Clear_clsCmd(JNIEnv *, jclass) { system("cls");//c's screen clear}4.5 Setting up the 64-bit DLL dynamic library to generate
Solution --->Properties
4.6 Generate dll
Solution --->Right-click to generate
5 Test
5.1 Copy clear.dll to the directory where Clear.class is located in steps 1 and 2.
5.2 Write test classes
public class TestJNI { public static void main(String[] args) { System.out.println("hello jni"); Clear.clsCmd();//Clear screen}}5.3 Compile test classes
javac TestJNI.java
5.4 Execute test class
java TestJNI
Perfect screen clear, get it done! ! !