[JNI] Von C aus auf eine Jave-Methode zugreifen mittels JNI



  • Ich versuche gerade mittels JNI von C aus auf eine Java Methode zuzugreifen.

    #include <stdio.h>
    #include <windows.h>
    #include "MpuTools.h"
    #include "jni.h"
    
    int main () {
    	JavaVM *jvm = NULL;
    	JavaVMInitArgs vm_args;
    	JavaVMOption options[2];
    	JNIEnv *env = NULL;
    
    	options[0].optionString = "-Djava.compiler=NONE";
    	options[1].optionString = "-Djava.class.path=.";
    
    	vm_args.version = JNI_VERSION_1_4;
    	vm_args.options = options;
    	vm_args.nOptions = 2;
    	vm_args.ignoreUnrecognized = JNI_FALSE; 	
    
    	printf("JNI_CreateJavaVM");
    	int res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
        if (res != JNI_OK) {
        	switch (res) {
        		case JNI_ERR:
    				printf("Error: %d\nunknown error", res);
    				break;
    			case JNI_EDETACHED:
    				printf("Error: %d\nthread detached from the VM", res);
    				break;
    			case JNI_EVERSION:
    				printf("Error: %d\nJNI version error", res);
    				break;
    			case JNI_ENOMEM:
    				printf("Error %d:\nnot enough memory", res);
    				break;
    			case JNI_EEXIST:
    				printf("Error %d:\nVM already created", res);
    				break;
    			case JNI_EINVAL:
    				printf("Error %d\ninvalid arguments ", res);
    				break;
        	}    	
        	return 0;
        }
        else {
        	printf("JVM created successfully");
        }	 	
    	return 0;
    }
    

    Leide rbekomme ich immer den Fehler JNI_ERR (unbekannter Fehler) zurück, mit dem ich nichts anfangen kann. Weiß da jemand weiter?



  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum Rund um die Programmierung verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Falls es helfen sollte, ich habe hier: http://www.michael-puff.de/files/JVM_Test.zip mal ein Demo-Programm hochgeladen. Ihr müsst nur noch die jvm.dll ins Programmverzeichnis kopieren.



  • In another post I found the solution to this problem,
    actually there are two solutions but this one is the simple one:

    If you include jni.h and link with jvm.lib your code will
    upon execution complain that it can't find jvm.dll, copying it
    to your working directory is NOT a good idear because it needs
    other files from the original directory!

    => SOLUTION: Include java\jre\bin\client or java\jre\bin\server in your
    PATH Environment variable! then restart your visual studio, it should
    then work 🙂


Anmelden zum Antworten