欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

被隐藏了的jni

程序员文章站 2022-06-12 20:00:12
...

public class HelloWorld {
static {

System.loadLibrary("Hello");
}
public native void DisplayHello();
public static void main(String[] args) {
new HelloWorld().DisplayHello();
}

}


javac HelloWorld.java
javah -jni HelloWorld
生成helloworld.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: DisplayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_DisplayHello
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

根据.h生成的[color=red]方法名[/color]写个cpp文件
HelloWorld.cpp

#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_DisplayHello
(JNIEnv *env, jobject obj)
{
printf("From jni_helloworldImpl.cpp :");
printf("Hello world ! \n");
return;
}

g++ -shared -I /usr/local/jdk/include/ -I /usr/local/jdk/include/linux/ -Wall -fPIC HelloWorld.cpp -o libHello.so
java -Djava.library.path=. HelloWorld

[color=red]注意一下要引入/usr/local/jdk/include/ 和/usr/local/jdk/include/linux/
还要加-Wall -fPIC[/color]
相关标签: jni