Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualMartins Mozeiko

Posted 15 May 2012 - 03:56 PM

Here is example of C++ code:
static int global = 0;

JNIEXPORT void JNICALL Java_com_example_YourClass_add(JNIEnv* env, jclass klass, jint value)
{
	global += value;
}

JNIEXPORT jint JNICALL Java_com_example_YourClass_getSum(JNIEnv* env, jclass klass)
{
	return global;
}

You wrap it with this Java code:
package com.example;

class YourClass
{
	static {
		System.loadLibrary("...");
	}

	public static native void add(int value);
	public static native int getSum();
}

Does that clear up things? Of course, its a bit bad example, because it uses global variables. But you can always return Java class instance (jobject) that stores needed data.

Java only clear memory for things allocated through JNI. You are resposnible for everything else (fopen, new, malloc, ...)

#1Martins Mozeiko

Posted 15 May 2012 - 03:55 PM

Here is example of C++ code:
static int global = 0;

JNIEXPORT void JNICALL Java_com_example_YourClass_add(JNIEnv* env, jclass klass, jint value)
{
    global += value;
}

JNIEXPORT jint JNICALL Java_com_example_YourClass_getSum(JNIEnv* env, jclass klass)
{
    return global;
}

You wrap it with this Java code:
package com.example;

static class YourClass
{
    static {
        System.loadLibrary("...");
    }

    public static native void add(int value);
    public static native int getSum();
}

Does that clear up things? Of course, its a bit bad example, because it uses global variables. But you can always return Java class instance (jobject) that stores needed data.

Java only clear memory for things allocated through JNI. You are resposnible for everything else (fopen, new, malloc, ...)

PARTNERS