Retrieve environment variable (JNI)[转]

Retrieve environment variable (JNI)

For some odd reasons, the getenv() method was removed from the JDK. Rumors is that a mechanism to retrieve an environment will be back in JDK1.5 (see this HowTo ). But for now, you can use -D switch to retrieve named environment variable and pass them to the JVM (see this HowTo ) or use this JNI routine :

JNIEXPORT jstring JNICALL JavaHowTo_getenv
  (JNIEnv *env, jclass c, jstring jname){
    if ( jname == NULL ) { 
       return NULL ; 
    }
    const char *name =
       (*env)->GetStringUTFChars(env, jname, (jboolean *)NULL) ;
    const char *value = getenv(name) ;
    (*env)->ReleaseStringUTFChars(env, jname, name) ;
    return value ? (*env)->NewStringUTF(env, value) : NULL ;
}

NOTE : This is fine if the environment variable contains only regular 7-bit ASCII characters.

See also this HowTo .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值