- 論壇徽章:
- 0
|
問題的關(guān)鍵在于用aCC編譯時的參數(shù).
根據(jù)HP網(wǎng)站上的兩篇文章可以很容易的使用JNI訪問傳統(tǒng)C++(Classical C++)程序:
http://www.hp.com/products1/unix/java/infolibrary/prog_guide/JNI_java2.html
http://forums1.itrc.hp.com/service/forums/questionanswer.do?admit=716493758+1092296929165+28353475&threadId=245738
但是,如果代碼中使用到了標(biāo)準(zhǔn)C++,也就是用到了STL,就會出現(xiàn)莫名其妙的JVM crash. 而且一般的現(xiàn)象是使用string的時候出錯.
最后發(fā)現(xiàn)是JVM的多線程機(jī)制和aCC編譯的缺省的多線程機(jī)制不一樣.所以編譯時需要加參數(shù)指定.
總的說來,編譯參數(shù)為:
OPTS=-AA +z +u4 -D_RWSTD_MULTI_THREAD -D_REENTRANT -D_HPUX -D_HPUX_SOURCE -D_POSIX_C_SOURCE=199506L -D_XOPEN_SOURCE_EXTENDED
其中,-D_RWSTD_MULTI_THREAD -D_REENTRANT 是指定多線程機(jī)制;同時必須添加-D_HPUX_SOURCE 參數(shù),否則,編譯時會出現(xiàn)奇怪的錯誤.
連接參數(shù)為:
-AA -b -lCsup_v2 -lstd_v2
值得注意的是根據(jù)上面所說的第二篇文章可知使用-AA編譯連接時,要連的庫是libCsup_v2.sl和libstd_v2.sl(這兩個庫是支持標(biāo)準(zhǔn)C++的庫),而不是第一篇文章中提到的libCsup.sl和libstd.sl(這兩個庫是支持傳統(tǒng)C++的庫).
另外,有幾個碰到的問題:
1. 如果編譯參數(shù)沒有指定多線程機(jī)制,禁用JIT(啟動JVM加參數(shù):-Djava.compiler=none -Xint )可以使簡單的例子通過,但是有些情況下還是會出錯.
2. 當(dāng)null作為String傳入JNI native接口代碼中是,使用env->GetStringUTFChars(jstring)會出現(xiàn)如下錯誤導(dǎo)致虛擬機(jī)崩潰:
Function=verify_instance_jfieldID__18jfieldIDWorkaroundSFP12klassOopDescP9_jfieldID
3. 在使用String作為JNI的傳入傳出參數(shù),使用GetStringUTFChars解決不了中文問題,還是會有亂碼. 正確的解決方法是使用以下兩個函數(shù):
void JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg)
{
jclass cls = env->FindClass(name);
/* if cls is NULL, an exception has already been thrown */
if (cls != NULL) {
env->ThrowNew(cls, msg);
}
/* free the local ref */
env->DeleteLocalRef(cls);
}
jstring JNU_NewStringNative(JNIEnv *env, const char *str)
{
if (str==NULL)
{
return NULL;
}
jclass jcls_str = env->FindClass("java/lang/String");
jmethodID jmethod_str = env->GetMethodID(jcls_str, "", "([B)V");
jstring result;
jbyteArray bytes = 0;
int len;
if (env->EnsureLocalCapacity(2) NewByteArray(len);
if (bytes != NULL) {
env->SetByteArrayRegion(bytes, 0, len,(jbyte *)str);
result = (jstring)env->NewObject(jcls_str, jmethod_str, bytes);
env->DeleteLocalRef(bytes);
return result;
} /* else fall through */
return NULL;
]
char *JNU_GetStringNativeChars(JNIEnv *env, jstring jstr)
{
jbyteArray bytes = 0;
jthrowable exc;
char *result = 0;
if (env->EnsureLocalCapacity(2) FindClass("java/lang/String");
jmethodID MID_String_getBytes = env->GetMethodID(jcls_str, "getBytes", "()[B"];
bytes = (jbyteArray)env->CallObjectMethod(jstr, MID_String_getBytes);
exc = env->ExceptionOccurred();
if (!exc) {
jint len = env->GetArrayLength( bytes);
result = (char *)malloc(len + 1);
if (result == 0) {
JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
0);
env->DeleteLocalRef(bytes);
return 0;
}
env->GetByteArrayRegion(bytes, 0, len, (jbyte *)result);
result[len] = 0; /* NULL-terminate */
} else {
env->DeleteLocalRef(exc);
}
env->DeleteLocalRef(bytes);
return (char*)result;
)
★注意:使用char *JNU_GetStringNativeChars()獲得的指針用完后要顯式的free().
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u/12951/showart_68851.html |
|