运行环境 centos
1.执行 javah xxx 生成xxx.h的头文件 这里生成Demo.h
demo.h
#include <jni.h>
/* Header for class Demo */
#ifndef _Included_Demo
#define _Included_Demo
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Demo(JNIEnv *,jobject);
#ifdef __cplusplus
}
#endif
#endif
demo.cpp
#include <iostream>
#include <jni.h>
#include "Demo.h"
using namespace std;
JNIEXPORT void Demo(JNIEnv* env,jobject* obj )
{
cout << "hello" << endl;
}
int main()
{
JNIEnv env;
jobject obj;
Demo(&env,&obj);
}
build: g++ Demo.cpp -I /usr/java/jdk1.8.0_141/include/ -I /usr/java/jdk1.8.0_141/include/linux -L /usr/java/jdk1.8.0_141/jre/lib/i386
本文介绍了一个使用JNI(Java Native Interface)的简单示例,通过创建一个名为Demo的类,演示了如何从Java调用C++代码。具体步骤包括:1) 使用javah工具生成Demo.h头文件;2) 在demo.h中声明Java方法;3) 在demo.cpp中实现该方法并输出'hello'。最后,通过g++编译器进行编译。
985

被折叠的 条评论
为什么被折叠?



