Java calls local C language methods via JNI

zhaozj2021-02-16  60

Java is very popular with its cross-platform features, but it is due to its cross-platform purpose, making it very little internal contacts and local machines, constrained its functions. One way to solve the Java to local operations is JNI.

Java calls the local method through the JNI, and the local method is stored in the form of a library file (on the Windows platform is a DLL file form, which is the form of SO files on the UNIX machine). By calling the internal method of the local library file, Java can implement close contact with the local machine, call the interface method of the system level.

Brief introduction and application is as follows:

First, the work you need to do in Java

In the Java program, you first need to declare the library name called in the class, as follows:

STATIC {

System.loadLibrary ("Goodluck");

}

Here, the expansion name of the library can be written, exactly the DLL or SO, which is determined by the system.

It also needs to make a local statement on the method to be called, the keyword is Native. And only need to be declared without the need for a specific implementation. as follows:

Public Native Static Void Set (INT I);

Public native static int GET ();

The Java program file is then compiled, generate a Class, and use the javah command, JNI will generate a header file of C / C .

For example, the program Testdll.java, the content is:

Public Class Testdll

{

Static

{

System.loadLibrary ("Goodluck");

}

Public native static int GET ();

Public Native Static Void Set (INT I);

Public static void main (string [] args)

{

TestDLL Test = New TestDLL ();

Test.set (10);

System.out.println (Test.get ());

}

}

Compile it with Javac TestDLL.java to generate TestDLL.CLASS.

Use Javah TestDLL, you will generate a TestDLL.h file in the current directory, which needs to be called to generate the required library files by the C / C program.

Second, the work you need to do in C / C

For generated .h header files, C / C needs to be done, is the implementation of its various methods. Then compile the connection to the library file. By copying the library file to the path to the Java program, you can call the functionality implemented by C / C with Java.

Connect an example. Let's take a look at the contents of the TestDLL.h file:

/ * Do Not Edit this file - IT is Machine Generated * /

#include

/ * Header for class testdll * /

#ifndef _included_testdll

#define _included_testdll

#ifdef __cplusplus

Extern "C" {

#ENDIF

/ *

* Class: Testdll

* Method: Get

* Signature: () i

* /

JNIEXPORT JINT JNICALL JAVA_TESTDLL_GET

(JNIENV *, JCLASS);

/ *

* Class: Testdll

* Method: SET

* Signature: (i) V

* /

JNIEXPORT VOID JNICALL JAVA_TESTDLL_SET (JNIENV *, JCLASS, JINT);

#ifdef __cplusplus

}

#ENDIF

#ENDIF

In the case of specific implementation, we only care about two function prototypes.

JNIEXPORT JINT JNICALL JAVA_TESTDLL_GET (JNIENV *, JCLASS);

with

JNIEXPORT VOID JNICALL JAVA_TESTDLL_SET (JNIENV *, JCLASS, JINT);

Here JNIEXPORT and JNICALL are keywords of JNI, indicating that this function is to be called by JNI. JINT is a type of int type of JNI with the local intangial INT communication with JNI, we can turn it, just do int use. The name of the function is Java_ plus the Package path of the Java program to add the functionality name. In the parameter, we only need to care about the parameters existing in the Java program. As for Jnienv * and JCLASS, we generally do not have to touch it.

Ok, let's take these two functions to implement these two functions with TestDLL.cpp files:

#include "testdll.h"

INT i = 0;

JNIEXPORT JINT JNICALL JAVA_TESTDLL_GET (JNIENV *, JCLASS)

{

Return I;

}

JNIEXPORT VOID JNICALL JAVA_TESTDLL_SET (JNIENV *, JCLASS, JINT J)

{

i = j;

}

Compile connection to the library file, this example is made under Windows, which is the DLL file. And the name should be consistent with Java, here is Goodluck.dll

Copy Goodluck.dll to the TestDLL.Class directory, Java TestDLL runs it, you can observe the result.

转载请注明原文地址:https://www.9cbs.com/read-17586.html

New Post(0)