Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Accessing global variables in native C code when calling from Java

843829Jul 19 2006 — edited Jul 27 2006
Hi, I wonder if you can help me please.

I'm calling the below function testBloom() from Java. In function test2(), the value of foo() is not 5, it's always some random huge number like 3432134 or -23278237. If I replace foo inside test2() with a literal value, all works fine as expected. This implies to me there's a problem accessing global variables in native C code when calling from JNI. Can you please tell me what the correct mechanism for getting the function to work as expected is?

#include <jni.h>
#include "bbapi.h"
#include "bbunix.h"

#include <stdio.h>
#include "tt.h"

int foo = 5;

int test2(int t) {
return (t + foo);

}


JNIEXPORT jint JNICALL Java_com_bear_calypsox_tk_marketdata_rt_BloombergFeedHandlerImpl_testBloom
(JNIEnv * env, jobject jobj, jint num) {

num = test2(num);
return num;

}

The reason for this simple example is that it describes what will happen when I call a function inside a very large C librbary -- the library utilises lots of global variables and strucutures and therefore will have the same issue surely? Can you please advise how people avoid this when calling into C libraries from Java?

Many thanks for your help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2006
Added on Jul 19 2006
5 comments
646 views