以下代码摘自网络一片文章,我只是加入更多的说明,方便一些入门的朋友。
luther@gliethttp:~/jni$ vim jusbhid.java
package gliethttp.usb.usbhid; // 使用打包命令package,将jusbhid类打包到gliethttp.usb.usbhid中.
public class jusbhid
{
public native String usbhid_open(int vid, int pid);
public native String usbhid_sendstring(String id, String command);
static {
System.loadLibrary("usbhid");
}
}
luther@gliethttp:~/jni$ javac jusbhid.java -d . // 将会在当前目录生成包路径gliethttp/usb/usbhid文件夹,如果没有定义-d .那么将直接在当前目录生成jusbhid.class
//没有实际作用,提示用
luther@gliethttp:~/jni$ tree gliethttp/
gliethttp/
`-- usb
`-- usbhid
`-- jusbhid.class
2 directories, 1 file
//这里一定要记住要退到gliethttp根目录上一层,如果你还在gliethttp/usb/usbhid/目录里面的话运行会报错。
luther@gliethttp:~/jni$ cd ../../../
luther@gliethttp:~/jni$ javah gliethttp.usb.usbhid.jusbhid // 生成jni头文件.h
luther@gliethttp:~/jni$ ll gliethttp_usb_usbhid_jusbhid.h // 头文件名为gliethttp_usb_usbhid_jusbhid.h
-rw-r--r-- 1 luther luther 788 2009-07-31 12:38 gliethttp_usb_usbhid_jusbhid.h
luther@gliethttp:~/jni$ vim gliethttp_usb_usbhid_jusbhid.h // 可以看到有如下内容,这里来看,加入package gliethttp.usb.usbhid;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* DO NOT EDIT THIS FILE - it is machine generated */ // 与直接定义public class gliethttp_usb_usbhid_jusbhid效果一样
#include <jni.h> // 类名中符号'_'表示包路径.
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Header for class gliethttp_usb_usbhid_jusbhid */
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#ifndef _Included_gliethttp_usb_usbhid_jusbhid
#define _Included_gliethttp_usb_usbhid_jusbhid
#ifdef __cplusplus
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
extern "C"
{
#endif
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* Class: gliethttp_usb_usbhid_jusbhid
* Method: usbhid_open
* Signature: (II)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_gliethttp_usb_usbhid_jusbhid_usbhid_1open
(JNIEnv *, jobject, jint, jint);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* Class: gliethttp_usb_usbhid_jusbhid
* Method: usbhid_sendstring
* Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_gliethttp_usb_usbhid_jusbhid_usbhid_1sendstring
(JNIEnv *, jobject, jstring, jstring);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
#ifdef __cplusplus
}
#endif
#endif
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
luther@gliethttp:~/jni$ vim jusbhid.c
// [luther.gliethttp] -- 20090731
#include <stdio.h>
#include "gliethttp_usb_usbhid_jusbhid.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
JNIEXPORT jstring JNICALL Java_gliethttp_usb_usbhid_jusbhid_usbhid_1open(JNIEnv *env, jclass obj, jint vid, jint pid)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
char buf[512];
printf("vid=0x%04x pid=0x%04x\n", vid, pid);
sprintf(buf, "0#1#2#3#4#5\n");
return (*env)->NewStringUTF(env, buf);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
JNIEXPORT jstring JNICALL Java_gliethttp_usb_usbhid_jusbhid_usbhid_1sendstring(JNIEnv *env, jclass obj, jstring id, jstring command)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int fd;
const char *idv;
const char *commands;
idv = ((*env)->GetStringUTFChars)(env, id, 0);
commands = ((*env)->GetStringUTFChars)(env, command, 0);
fd = atoi(idv);
printf("[%d] %s\n", fd, commands);
return (*env)->NewStringUTF(env, "usbhid_sendstring ok!\n");
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//上面创建好.h和.c文件后就要编译成.so的动态库了
luther@gliethttp:~/jni$ gcc -fPIC -I /home/xxx/jdk1.6.0_14/include -I /home/xxx/jdk1.6.0_14/include/linux -shared -o libusbhid.so jusbhid.c
//如果遇到环境问题,就看一下http://www.cppblog.com/noswimfish/archive/2010/12/07/135662.html//下面我们要写一个java的例子来测试我们生成JNI接口
luther@gliethttp:~/jni$ vim usbhid_jni_example.java
import gliethttp.usb.usbhid.*; // 导入CLASSPATH搜索路径中,路径为gliethttp/usb/usbhid/下的所有.class包
public class usbhid_jni_example
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public static void main(String[] args)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
String rets;
jusbhid hid = new jusbhid();
rets = hid.usbhid_open(0x1234,0x5678);
System.out.printf("%s", rets);
rets = hid.usbhid_sendstring("88", "QWS\r");
System.out.printf("%s", rets);
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
luther@gliethttp:~/jni$ javac usbhid_jni_example.java
//这里可能遇到编码问题,你可以适当的用下面的方式解决
luther@gliethttp:~/jni$ javac -encoding gbk usbhid_jni_example.java
//其中具体采用哪种编码根据的example中的例子的编码来觉得,我只举了一个gbk的例子,还有很多可以从网上查查。
//执行程序
luther@gliethttp:~/jni$ java usbhid_jni_example
vid=0x1234 pid=0x5678
0#1#2#3#4#5
[88] QWS
usbhid_sendstring ok!
//执行可能会出一些问题,可能找不到你之前生成 .so JNI动态库,你可以把动态库拷贝到/usr/lib目录下即可。