Java Native Interface – setting and retrieving object fields

My reference: Java Native Interface

This table was specifically handy:

Java Type JNI Type machine dependent
C/C++ typedef
Signature CallMethod
boolean jboolean unsigned char Z Boolean
byte jbyte signed char B Byte
char jchar unsigned short C Char
short jshort short S Short
int jint int I Int
long jlong long J Long
float jfloat float F Float
double jdouble double D Double
void void V Void
nonprimitive jobject * L...; Object


method definition signature
int m1 () ()I
double m2 (long l, char c) (JC)D
void m3 (String s, int[] a) (Ljava/lang/String;[I)V
String m4 (boolean b) (Z)Ljava/lang/String;
Object m4 (BigDecimal b) (Ljava/math/BigDecimal;)Ljava/lang/Object;

My code testing implementing this field access:

HelloWorldNative.c (builds into .so linked from Java)

JNIEXPORT void JNICALL Java_helloworld_Main_nativePrintNumber
(JNIEnv *env, jobject obj, jint ji)
jfieldID fid;
jint version;
jclass c;
c = (*env)->GetObjectClass(env, obj);
fid = (*env)->GetFieldID(env, c, "version_number", "I");
version = (*env)->GetIntField(env, obj, fid);
(*env)->SetIntField(env, obj, fid, ji*ji);
printf("::: %d, %dn", fid, version);

My, which the version_number integer field:

package helloworld;

* @author thomas
public class Main {
public int version_number;
private native void nativePrint();
private native void nativePrintNumber(int i);
/** Creates a new instance of Main */
public Main() {
version_number = 5;

static {
* @param args the command line arguments
public static void main(String[] args) {
Main me = new Main();
// TODO code application logic here