HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Managing Objects Between Java and C"
Vol. 9, Issue 10, p. 31

	



Listing 1: A Java class with a long variable that will hold the native objectÕs memory location

public class Session {

	public long nativeSession;

	public void native jniLogin(String server, String userName, String password);

	public void login(String server, String userName, String password) {
		jniLogin(server, userName, String password);
	}

}


Listing 2: Native code that fills the long variable of a Session class with
 native objects memory location

1:	JNIEXPORT void JNICALL Java_Session_jniLogin (JNIEnv * env, jobject obj,
 jstring server, jstring userName, jstring password) {
2:	const char *mserver;
3:	const char *muserName;
4:	const char *mpassword;
5:	jclass cls;
6:	jfieldID fid;
7:	jlong  sessionLocation;
8:         Session msession = NULL;
9:
10:       mserver = (*env)->GetStringUTFChars(env,server,0);
11:       muserName = (*env)->GetStringUTFChars(env,userName,0);
12:       mpassword = (*env)->GetStringUTFChars(env,password,0);
13:
14:       connect(mserver, muserName, mpassword, &msession);
15:	sessionLocation  =  (jlong)msession;
16:       (*env)->ReleaseStringUTFChars(env, server, mserver);
17:       (*env)->ReleaseStringUTFChars(env, userName, muserName);
18:    	(*env)->ReleaseStringUTFChars(env, password, mpassword);
19:
20:       cls = (*env)->GetObjectClass(env, obj);
21:       fid =(*env)->GetFieldID(env, cls,"nativeSession","J");
22:       (*env)->SetLongField(env,obj,fid,sessionLocation);
23: }


Listing 3: A Java class with another native method that uses the native
 session that gets created earlier

Public class Session {

	public long nativeSession;

	public void login(String server, String userName, String password) {
		jniLogin(server, userName, String password);
	}

	public void native jniLogin(String server, String userName, String password);

	public void sendMail(String to, String subject, String body) {
		jniSendMail(this, to, subject, body);
}

public void native jniSendMail(Object o,String to, String subject, String body);


}

Listing 4: Reusing the session object that is stored in the long variable of a session class

1:JNIEXPORT void JNICALL Java_Session_jniSendMail (JNIEnv * env, jobject obj,
 jobject sessionobj, jstring to, jstring subject, jstring body) {
2:	const char * mto;
3:	const char * msubject;
4:	const char * mbody;
5:	jclass cls;
6:	jfieldID fid;
7:	jlong session;
8:	Session * msession = NULL;
9:
10:       mto =  (*env)->GetStringUTFChars(env, to, 0);
11:       msubject =  (*env)->GetStringUTFChars(env, subject, 0);
12:       mbody =  (*env)->GetStringUTFChars(env, body, 0);
13:
14:	cls = (*env)->GetObjectClass(env, sessionobj);
15:       fid =(*env)->GetFieldID(env, cls,"nativeSession","J");
16:       session = (*env)->GetLongField(env,obj,fid);
17:
18:       msession = (Session)session;
19:       SendMail(msession, mto, msubject, mbody);
20:
21:       (*env)->ReleaseStringUTFChars(env, to, mto);
22:       (*env)->ReleaseStringUTFChars(env, subject, msubject);
23:       (*env)->ReleaseStringUTFChars(env, body, mbody);
24: }
  
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.