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!

Help calling java methods (primitive and object) from c (JNI)

843829Jan 27 2006 — edited Feb 2 2006
Hey,

I am trying to find the method (let's say primitive type such as int, bool, float, string and then move on to object). I have written a program in c which gets the int method and bool method of the java class; however, it doesn't get the float or string method of java class. Below is a java class followed by the actual JNI c program. If anyone is willing to help, it'll be greatly appreciated.


(this is an interface)

import java.lang.*;

public interface SampleInterface {

public int intMethod(int n);

public boolean booleanMethod(boolean bool);

public float floatMethod(float f);

public String stringMethod(String s);

// public Sample4 sampleMethod(int sample);

}


/***************************************************************/

(this is a java class which implements the above interface)

import java.lang.*;


public class Sample3 implements SampleInterface

{
private Sample4 s4;
private String str1 = "Hi";
private String str2 = "Bye";

public Sample3()
{

}

public int intMethod(int n)
{
return n*n;
}

public boolean booleanMethod(boolean bool)
{
return !bool;
}

public float floatMethod (float f)
{
return f * f;
}

public String stringMethod(String st)
{
return (str1 + st + "\n");
}

/*

public static void main(String args[])
{
System.out.println("\nTest main()\n");
}


public Sample4 sampleMethod(int k)
{
return new Sample4(k);
}

*/
}

/**************************************************************************************/

(this is the JNI c code which calls the above java class methods , ignore the commented methods)


/**********************************************************************
*
* (jniDriver3.c)----sample3.c (01/26/2006)
*
* This program is trying to get the int and bool method of the
* Sample3.java class.
*
*********************************************************************/

#include <jni.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define PATH_SEPARATOR ':' /* define it to be ':' on Linux, Solaris */
#define USER_CLASSPATH "/com/strongauth/symkey/skcl" /* where SKSImpl.class is */

main(int argc, char *argv[]) {
JNIEnv *env;
JavaVM *jvm;
jclass cls;
// jmethodID constr, gskmid;
jmethodID constr, midInt, midBl, midFlt, midSample3, midStr;
jint res, square;
jfloat flt;
jboolean not;
jobject obj;

// char *sd = "com/strongauth/symkey/skcl/SKSImpl";
// char *err = "An IOException occured!";

char *sd = "/home/ssuthar/symkey/jni/src/Sample3";

#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[1];

options[0].optionString = "-Djava.class.path=.";
// options[0].optionString = argv[1]; // Read in classpath through the command line
vm_args.version = 0x00010004;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
printf("\n\n");
printf("%s \n", "Executing the JNI_VERSION_1_2 block");
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);


#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);y

/* Append USER_CLASSPATH to the default system class path */

sprintf(classpath, "%s%c%s", vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
printf("classpath: %s \n", vm_args.classpath);

/* Create the Java VM */

res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
#endif /* JNI_VERSION_1_2 */

// JVM Status and JNI Version
printf("JVM Status: %d \n", res);
printf("Version: %x \n", (*env)->GetVersion(env));

if (res != JNI_ERR)
{
// Find and load the Java classes

cls = (*env)->FindClass(env, "Sample3");
printf("Classpath is: %s\n", sd);

if (cls == NULL) {
printf("Going to Destroy...\n");
goto destroy;
}

if(cls !=0)
{
// Found class

printf("Class Found: %s: OK\n", sd);
obj = (*env)->AllocObject(env, cls);
if(obj !=0){printf ("\nAllocating %s class object: OK \n", sd);}

//Get Constructor Method ID

constr = (*env)->GetMethodID(env, cls, "<init>", "()V");
if(constr !=0){printf ("\nGetting MethodID: Sample3 <constructor>: OK \n");}
else
{
printf("Can't find constr methodID: \n");
exit(1);
}

// Find intMethod

midInt = (*env)->GetMethodID(env, cls, "intMethod", "(I)I");
if(midInt != 0)
{
printf("\nGetting MethodID: intMethod: OK \n");
}

else
{
printf("\nCan't find int methodID: \n");
exit(1);
}

square = (*env)->CallIntMethod(env, obj, midInt, 6);
printf("Result of CallIntMethod: %d\n", square);


// Find booleanMethod

midBl = (*env)->GetMethodID(env, cls, "booleanMethod", "(Z)Z");
if(midBl != 0)
{
printf("\nGetting MethodID: booleanMethod: OK \n");
}

else
{
printf("\nCan't find bool methodID: \n");
exit(1);
}


not = (*env)->CallBooleanMethod(env, obj, midBl, 0);
printf("Result of CallBooleanMethod: %d\n", not);

// Find floatMethod

midFlt = (*env)->GetMethodID(env, cls, "floatMethod", "(F)F");

if(midFlt != 0)
{
printf("Getting MethodID: floatMethod: OK \n");
}

else
{
printf("\nCan't find floatMethod methodID: \n");
exit(1);
}

flt = (*env)->CallFloatMethod(env, obj, midFlt, 3.5);
printf("Result of CallFloatMethod: %d\n", flt);

/***************************************************

// Find sampleMethod

midSample3 = (*env)->GetMethodID(env, cls, "sampleMethod", "(I)L/home/ssuthar/jni/src/Sample4");
if(midSample3 != 0)
{
printf("\nGetting MethodID: sampleMethod: OK \n");
}

else
{
printf("\nCan't find sampleMethod methodID: \n");
exit(1);
}

jint square2 = (jint)(*env)->CallObjectMethod(env, obj, midSample3, 5);
printf("\nResult of CallObjectMethod: %d\n", square2);

***************************************************/

// Find stringMethod and convert a Java String into a C string

midStr = (*env)->GetMethodID(env, cls, "stringMethod", "(Ljava/lang/String)Ljava/lang/String");

if(midStr != 0)
{
printf("Getting MethodID: stringMethod: OK \n");
}

else
{
printf("\nCan't find stringMethod methodID: \n");
exit(1);
}
}
destroy:
printf("\nVM worked!\n");
printf("Destroying VM \n\n");
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}


(*jvm)->DestroyJavaVM(jvm);
return 0;
}
else
return -1;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 2 2006
Added on Jan 27 2006
4 comments
491 views