JNI 小解

本文记录了作者初次接触JNI的经历,通过实现一个简单的HelloWorld示例,介绍了如何声明本地方法并使用JDK工具生成C/C++头文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

唉,最近总是搞些开源的东东,接触的知识十分的乱,今天玩c/s的东东,明天搞服务器,也没有专注的去搞些东西。看看,今天又来了JNI,去网上逛了一圈,搞了些战利品。比葫芦画瓢,自己写了一个经典的 hello world!,简简单单的入门小例子,还请高手们多多指教。代码如下:
/**
* @author Andy
* 2011-7-20
* */
public class Sample1
{
/**
* 声明4个本地的方法,用于调用C或c++中的方法
* **/
public native int intMethod(int n);
public native boolean booleanMethod(boolean bool);
public native String stringMethod(String text);
public native int intArrayMethod(int[] intArray);

public static void main(String[] args)
{
System.loadLibrary("Sample1");//加载Sample1桥接文件
Sample1 sample = new Sample1();

int square = sample.intMethod(5);
boolean bool = sample.booleanMethod(true);
String text = sample.stringMethod("JAVA");
int sum = sample.intArrayMethod(
new int[]{1,1,2,3,5,8,13} );

System.out.println("intMethod: " + square);
System.out.println("booleanMethod: " + bool);
System.out.println("stringMethod: " + text);
System.out.println("intArrayMethod: " + sum);
}
}

用jdk中的javah,生成Sample1.h文件
-------------------未完-------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值