Strange Issue With Static Global Variable in Native

Chris Hegarty chris.hegarty at oracle.com
Sun Dec 8 01:34:45 PST 2013


On 07/12/2013 18:12, Dan Xu wrote:
> ...
>> Just so I understand, did you use a JNI global when caching the reference?
>>
>> -Alan
> Hi Alan,
>
> What is a JNI global?

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp1242

 > What I use here is a static global variable. I
> have uploaded this file to
> http://cr.openjdk.java.net/~dxu/8025437/DefaultProxySelector1.c. You can
> search for "no_proxy". I have declared this variable as "static jobject'
> at the beginning, and initialized it in function initJavaClass().

You can only store global, not local, refs across multiple calls/contexts.

In your case it would look something like:

static jobject setNoProxy(JNIEnv *env) {
               jobject empty_proxy = NULL;
               jfieldID pr_no_proxyID = NULL;
                 pr_no_proxyID = (*env)->GetStaticFieldID(env, proxy_class,
                         "NO_PROXY", "Ljava/net/Proxy;");
                 if (proxy_class && pr_no_proxyID) {
                     printf("create NO_PROXY\n");
                     empty_proxy = (*env)->GetStaticObjectField(env, 
proxy_class, pr_no_proxyID);

                     empty_proxy = (*env)->NewGlobalRef(env, empty_proxy);
                 }
                 return empty_proxy;
}

-Chris.



More information about the net-dev mailing list