java using JNI to call C under Linux++

Keywords: Java Linux JDK Windows

java using JNI to call C under Linux++

1.JNI introduction

JNI is Java Native Interface(Java local interface), which is a protocol. Its main function is to realize java calling C / C + + code (Class Library), or C / C + + calling java code

2. prepare JNI

First, use java h to generate. H header file according to. java file, and then use C + + to implement this interface

2.1 first create the JNI.java file in the directory

The contents are as follows

//JNI.java file
public class JNI{
   //Create a native interface method, which is implemented in C + + code
   public native int doSomething();
   //Static code block, load the. dll Dynamic link file generated by C + + code (. dll is equivalent to the jar package in Java...)
   static{
       System.loadLibrary("JNIDLL");
   }
}

2.2 use javac to compile JNI.java to generate JNI.class, and then use Java h to compile JNI.class to get JNI.h file. The steps are as follows

  • javac compilation
    javac JNI.java
  • javah compiles JNI.class (without suffix)
    javah JNI
  • Open JNI.h to view
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNI */

#ifndef _Included_JNI
#define _Included_JNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     JNI
 * Method:    doSomething
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_JNI_doSomething
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

2.3 write C + + code (JNIDLL.cpp) to implement the doSomething() method in JNI.java (native decoration)

Note that the method name is Java? JNI? Dosomething in JNI.h

//JNIdll.cpp file

#include<stdio.h>
#include<jni.h>
#include "JNI.h"

JNIEXPORT jint JNICALL Java_JNI_doSomething
  (JNIEnv *, jobject){
      //Implementation code
      int i = 777;
	  printf("in doSomething \n");
      return i;
}

2.4 using g + + compiler to generate. so dynamic library file

The command is as follows:
My jdk path is different from yours. You need to modify it when you use it

g++ -I /java_android/jdk1.8.0_131/include -I  /java_android/jdk1.8.0_131/include/linux/  -fPIC -shared -o libJNIDLL.so JNIDLL.cpp 

You can also copy jni.h and JNI ENU md.h to the current directory, and then compile:
The two files are under jdk1.8.0 and jdk1.8.0.131/include/linux/

g++  -fPIC -shared -o libJNIDLL.so JNIDLL.cpp 

3. use JNI

In the java main program, call the JNI file that is prepared above.

3.1 write the main program HelloWorld.java file

//HelloWorld.java file
import java.lang.Thread;
import java.util.Scanner;

public class HelloWorld {
    public static void main(String []args) {
        System.out.println("Hello World");
		//Create JNI's object call
        JNI jni = new JNI();
        int count =1;
        while (true)
        {
			try {
        		Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			//call() method;
			int i = jni.doSomething();
        	System.out.println("The first " + count + " second sleep 2s "); 
            count++;
	}
   }
}

3.2 compilation and operation

  • Compile
    javac HelloWorld.java
  • Function
    java HelloWorld

4. Complete process

virtual-machine:~/codeTest/javaTest$ ll
-rw-rw-r--  1 tsh tsh  534 Mar 29 17:03 HelloWorld.java
-rw-rw-r--  1 tsh tsh  229 Mar 29 17:07 JNIDLL.cpp
-rw-rw-r--  1 tsh tsh  309 Apr  1 11:14 JNI.java
virtual-machine:~/codeTest/javaTest$ javac JNI.java 
virtual-machine:~/codeTest/javaTest$ javah JNI
virtual-machine:~/codeTest/javaTest$ ll
-rw-rw-r--  1 tsh tsh  534 Mar 29 17:03 HelloWorld.java
-rw-rw-r--  1 tsh tsh  349 Apr  1 14:43 JNI.class
-rw-rw-r--  1 tsh tsh  229 Mar 29 17:07 JNIDLL.cpp
-rw-rw-r--  1 tsh tsh  354 Apr  1 14:44 JNI.h
-rw-rw-r--  1 tsh tsh  309 Apr  1 11:14 JNI.java
virtual-machine:~/codeTest/javaTest$ g++ -I /java_android/jdk1.8.0_131/include -I  /java_android/jdk1.8.0_131/include/linux/  -fPIC -shared -o libJNIDLL.so JNIDLL.cpp 
virtual-machine:~/codeTest/javaTest$ ll
-rw-rw-r--  1 tsh tsh  534 Mar 29 17:03 HelloWorld.java
-rw-rw-r--  1 tsh tsh  349 Apr  1 14:43 JNI.class
-rw-rw-r--  1 tsh tsh  229 Mar 29 17:07 JNIDLL.cpp
-rw-rw-r--  1 tsh tsh  354 Apr  1 14:44 JNI.h
-rw-rw-r--  1 tsh tsh  309 Apr  1 11:14 JNI.java
-rwxrwxr-x  1 tsh tsh 8136 Apr  1 14:46 libJNIDLL.so*
virtual-machine:~/codeTest/javaTest$ javac HelloWorld.java 
virtual-machine:~/codeTest/javaTest$ ll
-rw-rw-r--  1 tsh tsh  955 Apr  1 14:46 HelloWorld.class
-rw-rw-r--  1 tsh tsh  534 Mar 29 17:03 HelloWorld.java
-rw-rw-r--  1 tsh tsh  349 Apr  1 14:43 JNI.class
-rw-rw-r--  1 tsh tsh  229 Mar 29 17:07 JNIDLL.cpp
-rw-rw-r--  1 tsh tsh  354 Apr  1 14:44 JNI.h
-rw-rw-r--  1 tsh tsh  309 Apr  1 11:14 JNI.java
-rwxrwxr-x  1 tsh tsh 8136 Apr  1 14:46 libJNIDLL.so*
virtual-machine:~/codeTest/javaTest$ java HelloWorld 
Hello World
in doSomething 
//The first sleep 2s 
in doSomething 
//The second sleep 2s 
in doSomething 
//The third sleep 2s 

5. reference

Troubleshooting reference
Using JNI in windows

Posted by beermaker74 on Fri, 06 Dec 2019 11:59:34 -0800