char类型与jstring类型的转换

ichiko 2009-12-03 02:24:56
我有一个char *,现在想将其转换成jstring,请问应该怎样做呢?我对c不是很熟悉,请高手指!
...全文
1069 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzzhktk 2011-12-02
  • 打赏
  • 举报
回复
JNIEXPORT jstring JNICALL Java_com_xmcx_yw_jni_BiosTableReader_getDeviceType
(JNIEnv *env, jobject jo)
{
char DevType[MINSIZE]="";

getDeviceType(DevType, &bios_table);

return (*env)->NewStringUTF(env, DevType);
}

我是这么做的,可以参考一下
darxin 2009-12-05
  • 打赏
  • 举报
回复
参考一下brace的文章
关于SIGSEGV错误及处理方法
darxin 2009-12-05
  • 打赏
  • 举报
回复

jstring toJstring(JNIEnv* env, const char* pat) {
jclass strClass = env->FindClass("Ljava/lang/String;");
jmethodID ctorID =
env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}
ichiko 2009-12-03
  • 打赏
  • 举报
回复
有人回复吗?这个问题比较急啊~~~
ichiko 2009-12-03
  • 打赏
  • 举报
回复
TO:macrojj
我也是用这种方法做的,但是生成的so在java调用就会提示:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0xb775b0c9, pid=21196, tid=3075554192
#
# JRE version: 6.0_14-b08
# Java VM: Java HotSpot(TM) Client VM (14.0-b16 mixed mode, sharing linux-x86 )
# Problematic frame:
# V [libjvm.so+0x2210c9]
#
# An error report file with more information is saved as:
# /home/user/桌面/program/ictest/hs_err_pid21196.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
Java Result: 134

我的c++代码是这样的:
  #include<iostream>
#include<dlfcn.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "ictest.h"

JNIEXPORT jint JNICALL Java_ictest_auto_1init
(JNIEnv * env, jclass obj, jint a)
{
void* handle = dlopen("/home/user/Linux/mwic.so",RTLD_LAZY);
typedef int (*auto_init)(char*,int);
dlerror();
auto_init AutoInit = (auto_init) dlsym(handle,"auto_init");
const char *dlsym_error = dlerror();
if(dlsym_error){
dlclose(handle);
return -1;
}
int i=AutoInit("/home/user/Linux/star500x",a);
return i;
}


JNIEXPORT jstring JNICALL Java_ictest_rsc_14442
(JNIEnv * env, jclass obj, jint a, jint b)
{
int step=0;
void * handle = dlopen("/home/user/Linux/mwic.so",RTLD_LAZY);
step++;
typedef int (*rsc_4442)(int,int,char*);
step++;
dlerror();
step++;
rsc_4442 Rsc4442 =(rsc_4442) dlsym(handle,"rsc_4442");
step++;
const char *dlsym_error = dlerror();
step++;
if(dlsym_error){
step++;
dlclose(handle);
step++;
}
char* password;
step++;
Rsc4442(a,b,password);
step++;
jstring jstr;
step++;
jstr = stoJstring(env,password);
step++;
return jstr;
}
jstring stoJstring(JNIEnv* env, const char* pat)
{
jclass strClass = env->FindClass("Ljava/lang/String;");
jmethodID ctorID = env->GetMethodID(strClass, "\"init\"", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}


我的头文件是:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class ictest */

#ifndef _Included_ictest
#define _Included_ictest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ictest
* Method: auto_init
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_ictest_auto_1init
(JNIEnv *, jclass, jint);

/*
* Class: ictest
* Method: rsc_4442
* Signature: (II)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_ictest_rsc_14442
(JNIEnv *, jclass, jint, jint);


jstring stoJstring(JNIEnv* , const char*);

#ifdef __cplusplus
}
#endif
#endif


并且在linux下用“ g++ -wl,--kill-at -shared -o ictest.so ictest.cpp”这个命令生成so文件,请问我那里不对了呢?谢谢解答!
kostion 2009-12-03
  • 打赏
  • 举报
回复
string str(你的char*);
macrojj 2009-12-03
  • 打赏
  • 举报
回复
http://www.mscto.com/Javatec/01176202.html

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧