helloword.java.bak_JNI入门第一篇——HelloWord

本文详细介绍了JNI(Java Native Interface)的使用,从编写Java类声明native方法,到使用javah生成头文件,接着编写C代码实现方法,再到构建Android.mk文件编译生成SO库,最后在Java代码中加载并调用SO库的方法。整个过程清晰展示了Java与C/C++交互的流程。
摘要由CSDN通过智能技术生成

publicclassPrintf_Jni {

static{

System.loadLibrary("com_nedu_jni_helloword_printf-jni");

}

publicnativevoidprintHello();

}

2、javac编译

进入java文件所在路径,调用javac命令,如图:

0818b9ca8b590ca3270a3433284dd417.png

第二步:使用javah命令生成.h头文件,如图:

0818b9ca8b590ca3270a3433284dd417.png

这个要回到src目录下,不知道什么原因,如果在上面的javac路径下会报错,如图:

0818b9ca8b590ca3270a3433284dd417.png

使用javah命令生成的头文件如下:

/* DO NOT EDIT THIS FILE - it is machine generated */

#include 

/* Header for class com_nedu_jni_helloword_Printf_Jni */

#ifndef _Included_com_nedu_jni_helloword_Printf_Jni

#define _Included_com_nedu_jni_helloword_Printf_Jni

#ifdef __cplusplus

extern"C"{

#endif

/*

* Class:     com_nedu_jni_helloword_Printf_Jni

* Method:    printHello

* Signature: ()V

*/

JNIEXPORT voidJNICALL Java_com_nedu_jni_helloword_Printf_1Jni_printHello

(JNIEnv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

第三步:编写c文件,代码如下:

#include

#include 

#include "com_nedu_jni_helloword_Printf_Jni.h"

JNIEXPORT voidJNICALL Java_com_nedu_jni_helloword_Printf_1Jni_printHello

(JNIEnv *e, jobject j)

{

printf("Hello world!");

}

第四步,书写Android.mk文件,编译c文件

Android.mk文件如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := com_nedu_jni_helloword_printf-jni

LOCAL_SRC_FILES :=Printf_Jni.c

include $(BUILD_SHARED_LIBRARY)

LOCAL_MODULE    := com_nedu_jni_helloword_printf-jniLOCAL_MODULE    := com_nedu_jni_helloword_printf-jniLOCAL_MODULE  表示so文件名

LOCAL_SRC_FILES 需要编译的文件

第五步:使用so文件:

通过下面的代码加载so文件

System.loadLibrary("com_nedu_jni_helloword_printf-jni");

通过下面的代码加载so文件通过下面的代码加载so文件

调用如下:

Printf_Jni print=newPrintf_Jni();

print.printHello();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值