浅析如何将jni类打包package到指定的包路径中
浅析ubuntu 8.10下使用jdk6进行jni开发测试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文件夹,如果
luther@gliethttp:~/jni$ tree gliethttp/ // 没有定义-d .那么将直接在当前目录生成jusbhid.class
gliethttp/
`-- usb
`-- usbhid
`-- jusbhid.class
2 directories, 1 file
luther@gliethttp:~/jni$
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;
/* DO NOT EDIT THIS FILE - it is machine generated */ // 与直接定义public class gliethttp_usb_usbhid_jusbhid效果一样
#include
// 类名中符号'_'表示包路径.
/* Header for class gliethttp_usb_usbhid_jusbhid */
#ifndef _Included_gliethttp_usb_usbhid_jusbhid
#define _Included_gliethttp_usb_usbhid_jusbhid
#ifdef __cplusplus
extern "C" {
#endif
/*
* 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);
/*
* 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);
#ifdef __cplusplus
}
#endif
#endif
luther@gliethttp:~/jni$ vim jusbhid.c
// [luther.gliethttp] -- 20090731
#include
#include "gliethttp_usb_usbhid_jusbhid.h"
JNIEXPORT jstring JNICALL Java_gliethttp_usb_usbhid_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_gliethttp_usb_usbhid_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$ export CLASSPATH=.:$CLASSPATH // 如果没有正常配置jdk的话,需要强硬指定搜索路径
luther@gliethttp:~/jni$ sudo vim /etc/profile // 或者追加如下内容,配置jdk环境
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:~/jni$ vim usbhid_jni_example.java
import gliethttp.usb.usbhid.*; // 导入CLASSPATH搜索路径中,路径为gliethttp/usb/usbhid/下的所有.class包
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
usbhid_jni_example.java:7: cannot access jusbhid
bad class file: ./jusbhid.java
file does not contain class jusbhid
Please remove or make sure it appears in the correct subdirectory of the classpath.
jusbhid hid = new jusbhid();
^
1 error
luther@gliethttp:~/jni$ mv jusbhid.java jusbhid.java.raw // 必须去掉当前目录jusbhid.java,否则javac将提示上面的错误
luther@gliethttp:~/jni$ javac usbhid_jni_example.java
luther@gliethttp:~/jni$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
luther@gliethttp:~/jni$ java usbhid_jni_example
vid=0x1234 pid=0x5678
0#1#2#3#4#5
[88] QWS
usbhid_sendstring ok!
好了我们的jni类package打包分发工作初步探索已经告一段落了,因为有了package概念,
所以代码管理就更加容易,可以将一类的代码全部放入一个package包中.[luther.gliethttp]
阅读(4177) | 评论(0) | 转发(0) |