淺析如何將jni類打包package到指定的包路徑中
淺析ubuntu 8.10下使用jdk6進行jni開發(fā)測試
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 . // 將會在當(dāng)前目錄生成包路徑gliethttp/usb/usbhid文件夾,如果
luther@gliethttp:~/jni$ tree gliethttp/ // 沒有定義-d .那么將直接在當(dāng)前目錄生成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 // 可以看到有如下內(nèi)容,這里來看,加入package gliethttp.usb.usbhid;
/* DO NOT EDIT THIS FILE - it is machine generated */ // 與直接定義public class gliethttp_usb_usbhid_jusbhid效果一樣
#include <jni.h> // 類名中符號'_'表示包路徑.
/* 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 <stdio.h>
#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 // 或者追加如下內(nèi)容,配置jdk環(huán)境
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.*; // 導(dǎo)入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 // 必須去掉當(dāng)前目錄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打包分發(fā)工作初步探索已經(jīng)告一段落了,因為有了package概念,
所以代碼管理就更加容易,可以將一類的代碼全部放入一個package包中.[luther.gliethttp]
在VS2005下用C++寫的程序,在一臺未安裝VS2005的系統(tǒng)上,
用命令行方式運行,提示:
“系統(tǒng)無法執(zhí)行指定的程序”
直接雙擊運行,提示:
“由于應(yīng)用程序的配置不正確,應(yīng)用程序未能啟動,重新安裝應(yīng)用程序可能會糾正這個問題”
以前用VC6和VS2003的話, 如果缺少庫文件,是會提示缺少“**.dll”,但是用VS2005卻沒有這樣的提示。
自己實驗了一下,感覺以下幾種解決辦法是可行的:
方法一:
在類似C:\Program Files\Microsoft Visual Studio 8\VC\redi
st\Debug_NonRedist\x86\Microsoft.VC80.DebugCRT 下找到了下列文件:
msvcm80d.dll
msvcp80d.dll
msvcr80d.dll
Microsoft.VC80.DebugCRT.manifest
把這幾個文件拷貝到目標(biāo)機器上,與運行程序同一文件夾或放到system32下,就可以正確運行了。
其他release版、MFC程序什么的都是拷redist下相應(yīng)文件夾下的文件就可以了,文件夾后都有標(biāo)識!
方法二:
修改編譯選項,將/MD或/MDd 改為 /MT或/MTd,這樣就實現(xiàn)了對VC運行時庫的靜態(tài)鏈接,在運行時就不再需要VC的dll了。
方法三:
工程-》屬性-》配置屬性-》常規(guī)-》MFC的使用,選擇“在靜態(tài)庫中使用mfc”
這樣生成的exe文件應(yīng)該就可以在其他機器上跑了。
方法四:
你的vc8安裝盤上找到再分發(fā)包vcredist_xxx.exe和你的程序捆綁安裝