dcsimg
How to send Array to Java program from C and to get it back?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   itamar_c
Posted On:   Tuesday, October 29, 2002 07:44 AM

Hi everybody! * I dealing with a code from Sun->tutorial, invoke Java from c. There, they invoke a simple "Hello World" String. and I want to send Array of Float or Double to make some operations and get it back. and I can't make it!!! please help me. here is the original code: invoke.c file: #include #ifdef _WIN32 #define PATH_SEPARATOR ';' #else /* UNIX */ #define PATH_SEPARATOR ':' #endif #define USER_CLASSPATH "." /* where Prog.class is */ int main() { JNIEnv *env; JavaVM *jvm; JDK1_1InitArgs vm_args; jint res;    More>>

Hi everybody!

* I dealing with a code from Sun->tutorial, invoke Java from c. There, they invoke a simple "Hello World" String. and I want to send Array of Float or Double to make some operations and get it back.
and I can't make it!!! please help me.

here is the original code:

invoke.c file:

#include


#ifdef _WIN32

#define PATH_SEPARATOR ';'

#else /* UNIX */

#define PATH_SEPARATOR ':'

#endif


#define USER_CLASSPATH "." /* where Prog.class is */


int main() {


JNIEnv *env;

JavaVM *jvm;

JDK1_1InitArgs vm_args;

jint res;

jclass cls;

jmethodID mid;

jstring jstr;

jobjectArray args;

char classpath[1024];



/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */

vm_args.version = 0x00010001;


JNI_GetDefaultJavaVMInitArgs(&vm_args);


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

sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);

vm_args.classpath = classpath;


/* Create the Java VM */

res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);

if (res < 0) {

fprintf(stderr, "Can't create Java VM
");

exit(1);

}


cls = (*env)->FindClass(env, "Prog");

if (cls == 0) {

fprintf(stderr, "Can't find Prog class
");

exit(1);

}


mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");

if (mid == 0) {

fprintf(stderr, "Can't find Prog.main
");

exit(1);

}


jstr = (*env)->NewStringUTF(env, " from C!");

if (jstr == 0) {

fprintf(stderr, "Out of memory
");

exit(1);

}


args = (*env)->NewObjectArray(env, 1,(*env)->FindClass(env, "java/lang/String"), jstr);


if (args == 0) {

fprintf(stderr, "Out of memory
");

exit(1);

}

(*env)->CallStaticVoidMethod(env, cls, mid, args);

(*jvm)->DestroyJavaVM(jvm);

}



Prog.java file

public class Prog {


public static void main(String[] args) {

System.out.println("Hello World "+ args[0]);


}


-------------------------------------------------

** In addition, how can I get API I mean the all function that JNI have.
Please HELP ME?!


thanks, Itamar C.
itamarc@hotmail.com    <<Less
About | Sitemap | Contact