Jump to content

  • Log In with Google      Sign In   
  • Create Account


JNI question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Aliii   Members   -  Reputation: 1445

Like
0Likes
Like

Posted 15 May 2012 - 02:31 AM

Ive just started to learn JNI. It seems like: I call a native function, the function does its job, returns something, and thats all. Is there a way to make the native part "remember" data, that I send from Java? ....so I dont have to send it in every iteration, when its constant data. Can I make a native thread, and use "global native variables" with JNI? Thanks very much!

Sponsor:

#2 frob   Moderators   -  Reputation: 20478

Like
0Likes
Like

Posted 15 May 2012 - 09:03 AM

JNI is just a function call, nothing more. It is no different than calling a function in Java or C++ or python or perl or whatever other language you are interfacing with.

You'd need to write your native library in such a way that it keeps a copy of the data. Otherwise you'll need to just keep sending a copy every time.
Check out my personal indie blog at bryanwagstaff.com.

#3 Aliii   Members   -  Reputation: 1445

Like
0Likes
Like

Posted 15 May 2012 - 03:47 PM

You'd need to write your native library in such a way that it keeps a copy of the data.

Thanks! But how can I do that? Im using android NDK, trying to write cpp functions. It compiles the cpp files, puts the .so files into the project/jni folder, and then I can call the functions ....thats all I know about it. How Java calls the native functions, how it "switches" from VM to native, where are the native functions/data in the memory, does Java clear that memory after the functions return ....I dont know.

#4 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
1Likes
Like

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;

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, ...)

Edited by Martins Mozeiko, 15 May 2012 - 03:56 PM.


#5 Aliii   Members   -  Reputation: 1445

Like
0Likes
Like

Posted 17 May 2012 - 12:54 AM

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, ...)


Thanks! It works




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS