Simple use of native keyword in Java

Posted by friendlylad on Fri, 04 Mar 2022 08:08:48 +0100

1. Introduction to native keyword

In principle, 100% pure java code is very good, but sometimes you also want to use code in other languages, which is called local code. The provision of local code allows us to use the code written in (c/c + +) in Java, and the implementation of this function depends on the API provided by the Java platform to interoperate with the local (c/c + +) code - JNI(Java Native Interface), which is called Java local interface.

2. Some suggestions on the use of native keywords

  • The system features and devices that your application needs to access cannot be realized through the Java platform.
  • You already have a lot of tested and debugged code written in another language and know how to export it to all target platforms.
  • Through benchmarking, you find that the Java code you write is much slower than the equivalent code written in other languages.

3. An example of native usage

To briefly illustrate the usage of native, we output Hello World! For example.

(1) Preparation before operation:

  • This example uses Windows 10 system for operation.
  • JDK version: 14.0.2
  • MinGW version: 8.1.0 (x86_64-posix-seh-rev0, build by mingw-w64 project)
  • All files of this instance will be organized in D:\Native.

(2) Create the java file hellonative. In D:\Native Java and write the following contents in the file:

class HelloNative {
	public static native void green();
}

(3) Compile the above files and check the directory. You can find that a class file has been generated.

D:\Native>javac HelloNative.java
D:\Native>dir
 Driver D The volume in is WPKY
 The serial number of the volume is 1 C7F-A8C8

 D:\Native Directory of

2021/04/17  17:27    <DIR>          .
2021/04/17  17:27    <DIR>          ..
2021/04/17  17:27               212 HelloNative.class
2021/04/17  17:25                59 HelloNative.java
               2 Files            271 bytes
               2 Directories 73,375,858,688 Available bytes

(4) Use javac - H The file command automatically generates header files in the parent directory.
be careful:

  • There is a space between. And - h and file.
  • If your JDK version is less than JDK 10, this version should use javah
    Replace java -h, because javah has been deleted in JDK10 and above.
  • If there is no between - h and file, The following information will be prompted:
D:\Native>javac -h HelloNative.java
 error: Passive file
  • The file suffix must be added java, otherwise the following information will be prompted:
D:\Native>javac -h . HelloNative
 error: Class names are accepted only when annotation processing is explicitly requested 'HelloNative'
1 Errors

Use javac - H correctly HelloNative. The following parent directory will be generated automatically after the Java command:


The contents of the header file are as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloNative */

#ifndef _Included_HelloNative
#define _Included_HelloNative
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloNative
 * Method:    green
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloNative_green
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

(5) Create a C source file in the same level directory, and copy the following code in the header file to the source file just created.

JNIEXPORT void JNICALL Java_HelloNative_green
  (JNIEnv *, jclass);

#ifdef __cplusplus
}

The final content of the created C source file is as follows:

#include "HelloNative.h"
#include<stdio.h>
JNIEXPORT void JNICALL Java_HelloNative_green(JNIEnv * env, jclass cl){
	printf("Hello World!");
}

be careful:

  • To (JNIEnv *, jclass); Replace the semicolon of with {, and add env and cl to the parameter list at the same time, otherwise the following information will be prompted when compiling to generate a shared file:
D:\Native>gcc -m64 -Wl,--add-stdcall-alias -I 
HelloNative.dll HelloNative.c
HelloNative.c: In function 'Java_HelloNative_green':
HelloNative.c:3:47: error: parameter name omitted
 JNIEXPORT void JNICALL Java_HelloNative_green(JNIEnv *, jclass){
                                               ^~~~~~~~
HelloNative.c:3:57: error: parameter name omitted
 JNIEXPORT void JNICALL Java_HelloNative_green(JNIEnv *, jclass){

(6) Compile to produce shared files.
Use the command GCC - M64 - WL, -- add stdcall alias - I JDK \ include - I JDK \ include \ Win32 \ - shared - O hellonative dll HelloNative. C generate shared files, where JDK represents your JDK installation path, that is, the parent directory of bin directory in JDK. Do not select bin directory. After the correct compilation, hellonative. XML will be generated in the same level directory DLL file. As shown in the figure:

(7) Create a java file to test.

class HelloNativeTest{
	static
	{
		System.loadLibrary("HelloNative");
	}
	public static void main(String[] args){
		HelloNative.green();
	}
}

(8) Compile and run the file.

Finally, "Hello World!" will be output in the console.

Topics: Java