Re: Calling .jar routines from a C program
Re: Calling .jar routines from a C program
- Subject: Re: Calling .jar routines from a C program
- From: Andrei Tchijov <email@hidden>
- Date: Sat, 1 Apr 2006 12:35:29 -0500
Take a look at this. In this example I am invoking static method
"main" from class "YOUCLASS" and passing "C" argv to it. Read http://
java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html for more
information (how to create objects and invoke none-static methods )
#include <jni.h>
int main( int argc, char** argv ) {
JavaVM* jvm; /* denotes a Java VM */
JNIEnv* env; /* pointer to native method interface */
JavaVMInitArgs vm_args;
JavaVMOption options[10];
int oi = 0;
options[oi++].optionString = "-Djava.class.path=YOUR.jar:/System/
Library/Frameworks/JavaVM.framework/Classes/classes.jar";
options[oi++].optionString = "-Djava.library.path=/System/
Library/Frameworks/JavaVM.framework/Libraries";
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = oi;
vm_args.ignoreUnrecognized = 1;
// setenv("JAVA_JVM_VERSION", "1.5", 1);
JNI_CreateJavaVM( &jvm, (void**)&env, &vm_args );
jclass stringClazz =(*env)->FindClass( env, "java/lang/
String");
jclass serverClazz = (*env)->FindClass( env, "YOURCLASS");
jmethodID mid = (*env)->GetStaticMethodID( env, serverClazz,
"main", "([Ljava/lang/String;)V");
jarray jArgv = (*env)->NewObjectArray( env, argc-1, stringClazz,
NULL );
int i;
for( i = 1; i < argc; i++ ) {
jstring jArg = (*env)->NewStringUTF( env, argv[ i ] );
(*env)->SetObjectArrayElement( env, jArgv, i-1, jArg );
}
(*env)->CallStaticVoidMethod(env, serverClazz, mid, jArgv );
(*jvm)->DestroyJavaVM( jvm );
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden