1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <stdio.h>
#include <jni.h>
#include <stdlib.h>
#define USER_CLASSPATH "D:\\eclipse\\workspace\\TEST\\wrapperCtoJava\\src"
int main(int nm, char * tm[])
{
JNIEnv *penv;
JavaVM *jvm;
JavaVMInitArgs vm_args;
jint res;
jclass i_cls; // identité de la classe
jmethodID i_mth; // identité de la méthode
jstring j_ch; // pour créer une chaîne 'java'
JavaVMOption options[1];
options[0].optionString = "-Djava.class.path=" USER_CLASSPATH;
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Créer la machine virtuelle Java */
printf("Creation Java Virtual Machine \n");
res = JNI_CreateJavaVM(&jvm,(void **)&penv,&vm_args);
if (res < 0) {
fprintf(stderr, "Machine virtuelle non démarée !!!\n");
exit(1);
}
printf("Result of JVM : %i \n", res);
// Récupérer la classe
printf("Find Class : Fichier \n");
i_cls = (*penv)->FindClass(penv,"Fichier");
if (i_cls == NULL) {
fprintf(stderr, "Class not found\n");
goto destroy;
}
printf("Result of Classe : %i \n", i_cls);
// Récupérer la méthode
printf("Get Method : setAttribut \n");
i_mth = (*penv)->GetMethodID(penv, i_cls, "setAttribut", "(Ljava/lang/String;)V");
if (i_mth == NULL) {
fprintf(stderr, "Methode Fichier.setAttribut not found\n");
goto destroy;
}
printf("Result of Method : %i \n", i_mth);
printf("Creation String \n");
j_ch = (*penv)->NewStringUTF(penv, "[C] blabla ");
if (j_ch == NULL) {
fprintf(stderr, "End of memory !!!\n");
goto destroy;
}
printf("Result of creation Srting : %i \n",j_ch);
printf("Call Method \n");
(*penv)->CallVoidMethod(penv, i_cls, i_mth, j_ch);
destroy:
if ((*penv)->ExceptionOccurred(penv)) {
(*penv)->ExceptionDescribe(penv);
}
printf("Destruction Java Virtual Machine \n");
(*jvm)->DestroyJavaVM(jvm);
} |
Partager