浅析ubuntu 8.10下使用jdk6进行jni开发测试
浅析如何将jni类打包package到指定的包路径中首先下载jdk6:
1.安装配置jdk6
luther@gliethttp:~$ chmod +x jdk-6u14-linux-i586.bin
luther@gliethttp:~$ ./jdk-6u14-linux-i586.bin
luther@gliethttp:~$ sudo mv jdk1.6.0_14 /usr/local/
luther@gliethttp:~$ sudo vim /etc/profile 追加如下几行内容
JAVA_HOME=/usr/local/jdk1.6.0_14
JRE_HOME=/usr/local/jdk1.6.0_14/jre
CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export JAVA_HOME JRE_HOME CLASSPATH
luther@gliethttp:~$ source /etc/profile
luther@gliethttp:~$ sudo vim ~/.bashrc 追加如下一行内容
export PATH=/usr/local/jdk1.6.0_14/bin:$PATH
luther@gliethttp:~$ . ~/.bashrc
luther@gliethttp:~$ java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)
好了,jdk已经配置完成了,我们来写jni.
2.jni声明以及.h文件生成
luther@gliethttp:~/jni$ cat jusbhid.java
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
luther@gliethttp:~/jni$ javah jusbhid
luther@gliethttp:~/jni$ ls
jusbhid.class jusbhid.h jusbhid.java
luther@gliethttp:~/jni$ vim jusbhid.c
// [luther.gliethttp] -- 20090730
#include
#include "jusbhid.h"
JNIEXPORT jstring JNICALL Java_jusbhid_usbhid_1open(JNIEnv *env, jclass obj, jint vid, jint pid)
{
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);
}
JNIEXPORT jstring JNICALL Java_jusbhid_usbhid_1sendstring(JNIEnv *env, jclass obj, jstring id, jstring command)
{
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");
}
luther@gliethttp:~/jni$ gcc -fPIC -I /usr/local/jdk1.6.0_14/include -I /usr/local/jdk1.6.0_14/include/linux -shared -o libusbhid.so jusbhid.c
luther@gliethttp:~/jni$ vim usbhid_jni_example.java
public class usbhid_jni_example
{
public static void main(String[] args)
{
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);
}
}
luther@gliethttp:~/jni$ javac usbhid_jni_example.java
luther@gliethttp:~/jni$ ls
jusbhid.c jusbhid.class jusbhid.h jusbhid.java libusbhid.so usbhid_jni_example.class usbhid_jni_example.java
luther@gliethttp:~/jni$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
luther@gliethttp:~/jni$ strip -s libusbhid.so 可以极大减少so大小
luther@gliethttp:~/jni$ java usbhid_jni_example
vid=0x1234 pid=0x5678
0#1#2#3#4#5
[88] QWS
usbhid_sendstring ok!
luther@gliethttp:~/jni$
|
文件: | jusbhid.tar.bz2 |
大小: | 1KB |
下载: | 下载 |
|
阅读(2538) | 评论(0) | 转发(0) |