android JNI 调用 >GetStringUTFChars报错

http://blog.csdn.net/xzx735/article/details/23679333


 Code Example 2-1 Implementing a Native Method Using C

jdouble Java_pkg_Cls_f__ILjava_lang_String_2 (
     JNIEnv *env,        /* interface pointer */
     jobject obj,        /* "this" pointer */
     jint i,             /* argument #1 */
     jstring s)          /* argument #2 */
{
     /* Obtain a C-copy of the Java string */
     const char *str = (*env)->GetStringUTFChars(env, s, 0);
     /* process the string */
     ...
     /* Now we are done with str */
     (*env)->ReleaseStringUTFChars(env, s, str);
     return ...
}

Note that we always manipulate Java objects using the interface pointer env . Using C++, you can write a slightly cleaner version of the code, as shown in Code Example 2-2:
Code Example 2-2 Implementing a Native Method Using C++

extern "C" /* specify the C calling convention */  

jdouble Java_pkg_Cls_f__ILjava_lang_String_2 (
     JNIEnv *env,        /* interface pointer */
     jobject obj,        /* "this" pointer */
     jint i,             /* argument #1 */
     jstring s)          /* argument #2 */
{
     const char *str = env->GetStringUTFChars(s, 0);
     ...
     env->ReleaseStringUTFChars(s, str);
     return ...

jni意常处理

cpp

Android.mk

LOCAL_SHARED_LIBRARIES  := libc libutils libcutils libnativehelper

#include "JNIHelp.h"
#include <jni.h>
#include <utils/Log.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <linux/ioctl.h>

#define LOG_TAG "run-system-cmd"

namespace android
{

JNIEXPORT jint JNICALL Java_com_forlinx_android_HardwareInterface_runSystemCmd
(JNIEnv *env, jobject obj,jstring jcmd)
{
    int ret;
    const char *cmd = NULL;

    cmd = jcmd ? env->GetStringUTFChars(jcmd, NULL) : NULL;

    if (!cmd) {
         jniThrowNullPointerException(env, "cmd");
         return -EINVAL;
    }

    ret = system(cmd);

    if(ret < 0) {
        ALOGW("Unable to run cmd=%s  %s\n",cmd,strerror(errno));
        ret = -1;
    }

    env->ReleaseStringUTFChars(jcmd, cmd);

    return ret;
}

}//end namespace android
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值