JNI Demo In Linux

Overview

JNI (Java Native Interface) is used to call C/C++ libraries by Java code.

The C/C++ library is known as SO library in Linux and is known as DLL in windows.

Environment Requirment

  • Linux
  • GCC
  • G++
  • Java 1.8+

If you have not prepared for the environment, you can install them by commands below:

yum -y install gcc
yum -y install gcc+ gcc-c++
yum -y install java-1.8.0-openjdk-devel.x86_64

Let’s Start

First, I prepared a Java file called JniDemo.java and its content is below:

[root@snail jni_helloworld]# cat JniDemo.java 
public class JniDemo{

  public static void main(String[] args){
    System.out.println("Hello, I'm Java!");
    new JniDemo().hello();
  }

  public native void hello(); // Native key word tell Jvm that the method is from SO libraries instead of Java.

  static{
    System.loadLibrary("cpp_lib_demo"); // Loading the C library.
  }
}

Second, use javah tool to generate JniDemo.h file.

[root@snail jni_helloworld]# javah JniDemo
[root@snail jni_helloworld]# ls
JniDemo.h  JniDemo.java

Third, It’s time to code our cpp file.

[root@snail jni_helloworld]# cat cpp_demo.cpp 
#include "JniDemo.h" // Import generated header file.
#include "jni.h"
JNIEXPORT void JNICALL Java_JniDemo_hello
  (JNIEnv * env, jobject jb){
      printf("%s","Hello, I'm Cpp.\n");
}

To compile our cpp file, we need to make our Makefile file.

[root@ jni_helloworld]# cat Makefile 
libcpp_lib_demo.so : cpp_demo.cpp
	g++ -o $@ $+ -fPIC -shared -I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/include -I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/include/linux

.PHONY : clean
clean :
	rm libcpp_lib_demo.so

Note: You need to modify the java path to your java path.

And now, let’s us to compile our Java file to class and compile our cpp file to SO library.

 [root@snail jni_helloworld]# javac JniDemo.java 
 [root@snail jni_helloworld]# make
g++ -o libcpp_lib_demo.so cpp_demo.cpp -fPIC -shared -I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/include -I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/include/linux
 [root@snail jni_helloworld]# ls
cpp_demo.cpp  JniDemo.class  JniDemo.h  JniDemo.java  libcpp_lib_demo.so  Makefile

Now, we can run our JNI program.

[root@snail jni_helloworld]# java -Djava.library.path='.' JniDemo
Hello, I'm Java!
Hello, I'm Cpp.
当您在Java程序中需要调用C/C++代码时,可以使用Java Native Interface(JNI)来实现。下面是一个简单的JNI示例,演示了如何将Java方法与C函数相互调用: 1. 创建一个Java类,例如"JNIDemo.java",其中包含您想要调用的本地方法: ```java public class JNIDemo { // 本地方法声明 public native void sayHello(); // 加载本地库 static { System.loadLibrary("jni_demo"); // 加载名为"jni_demo"的本地库 } // 测试 public static void main(String[] args) { JNIDemo demo = new JNIDemo(); demo.sayHello(); // 调用本地方法 } } ``` 2. 在命令行中使用`javac`编译Java类:`javac JNIDemo.java`。 3. 生成C头文件,可以使用`javah`工具:`javah JNIDemo`。这将生成名为"JNIDemo.h"的头文件。 4. 创建一个C源文件,例如"jni_demo.c",实现您在Java中声明的本地方法: ```c #include <stdio.h> #include "JNIDemo.h" JNIEXPORT void JNICALL Java_JNIDemo_sayHello(JNIEnv *env, jobject obj) { printf("Hello from C!\n"); } ``` 5. 在命令行中使用C编译器编译C源文件,并生成共享库文件(DLL或SO): - 对于Windows(使用MinGW):`gcc -shared -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" jni_demo.c -o jni_demo.dll` - 对于Linux/Mac:`gcc -shared -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" jni_demo.c -o libjni_demo.so` 注意:请将`$JAVA_HOME`替换为您Java安装的实际路径。 6. 运行Java程序:`java JNIDemo`。您将看到输出:"Hello from C!"。 这是一个简单的JNI示例,演示了如何在Java和C之间进行方法调用。您可以根据自己的需求扩展和定制此示例。希望对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iioSnail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值