Search code examples
javac++jvmjava-native-interface

Calling main method from JNI fails


I created a C++ class that is supposed to call Main.main by following: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#wp9502.

I didn't get it to work so I followed: http://www.coderanch.com/t/525082/CPP/create-JVM-native-code-call

and :

imp_JNI_Crea">http://www.codeproject.com/Questions/263687/Linker-error-undefined-reference-to-imp_JNI_Crea

None of which worked. So I changed my code back to what the Invocation API article by oracle says (the first link).

My C++ code looks like:

In JNI.hpp file:

#include <jni.h>
#include <windows.h>
#include <iostream>

class Jvm
{
private:
    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs jvm_args;
    JavaVMOption* options;

public:
    Jvm();
};

In JNI.cpp file:

Jvm::Jvm()
{
    options = new JavaVMOption[3];

    options[0].optionString = "-Djava.compiler=NONE";
    options[1].optionString = "-Djava.class.path=C:/Users/Brandon/Documents/NetBeansProjects/Loader/build/classes";
    options[2].optionString = "-verbose:class";

    jvm_args.version = JNI_VERSION_1_6;
    jvm_args.nOptions = 3;
    jvm_args.options = options;
    jvm_args.ignoreUnrecognized = false;

    //JNI_GetDefaultJavaVMInitArgs(&jvm_args);
    JNI_CreateJavaVM(&jvm, reinterpret_cast<void**>(&env), &jvm_args);

    jclass MainClass = env->FindClass("loader.Main");


    //Crashes on the next line:
    jmethodID MainMethod = env->GetStaticMethodID(MainClass, "main", "([Ljava/lang/String;)V");

    MessageBox(NULL, "", "", 0);

    Sleep(1000);

    jvm->DestroyJavaVM();
    delete[] options;
}

My java code looks like:

package loader;

public class Main {

    public static void main(String[] args) {
        //JavaProcess.exec(ClientApplet.class);
        System.out.println("Hello!");
    }
}

And the verbose prints:

[Loaded loader.Main from file:/C:/Users/Brandon/Documents/NetBeansProjects/Loader/build/classes/]

Process returned -1073741571 (0xC00000FD)   execution time : 1.730 s
Press any key to continue.

What am I doing wrong? Why does it fail to call the method? The JNI.dll that I loaded is from: C:\Program Files\Java\jdk1.7.0_21\jre\bin\server\jvm.dll because the latest Java 7u25 doesn't have a bin\client\jvm.dll.

I even statically linked to the jvm.lib: C:\Program Files\Java\jdk1.7.0_21\lib\jvm.lib.


Solution

  • jclass MainClass = env->FindClass("loader.Main");
    

    This is wrong. You have to use slashes instead of dots when using JNI functions, just like in method signatures.

    The correct code is:

    jclass MainClass = env->FindClass("loader/Main");