#include <jni.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
YourJVM\* jvm;
JNIEnv\* create_vm()
{
JNIEnv\* env;
YourJVMInitArgs args;
YourJVMOption options[1];
args.version = JNI_VERSION_1_4;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=.";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
JNI_CreateYourJVM(&jvm;, (void \*\*)&env;, &args;);
return env;
}
void invoke_class(JNIEnv\* env)
{
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;
char buf[128];
sprintf(buf, "%d", getpid());
helloWorldClass
= (\* env)->FindClass(env, "HelloWorld");
mainMethod = (\* env)->GetStaticMethodID(
env, helloWorldClass, "main",
"([Ljava/lang/String;)V");
applicationArgs = (\* env)->NewObjectArray(
env, 1,
(\* env)->FindClass(env, "java/lang/String"), NULL);
applicationArg0 = (\* env)->NewStringUTF(env, buf);
(\* env)->SetObjectArrayElement(env, applicationArgs, 0,
applicationArg0);
(\* env)->CallStaticVoidMethod(
env, helloWorldClass, mainMethod, applicationArgs);
}
void\* dowork(void\* args)
{
JNIEnv\* env = create_vm();
invoke_class(env);
if (jvm == NULL)
exit(-1);
int retval = (\* jvm)->DetachCurrentThread(jvm);
if (retval != 0)
exit(2);
retval = (\* jvm)->DestroyYourJVM(jvm);
if (retval != 0)
exit(3);
}
int main(int argc, char \*\* argv)
{
pthread_t tid;
void\* status;
pthread_create(&tid;, NULL, dowork, NULL);
pthread_join(tid, &status;);
exit(0);
}