Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to `JNI_CreateJavaVM' windows

I'm trying to get familiar with the JNI API but can't get a sample c++ program to compile. I got the same sample to compile and run in linux (after posting the question in the link below) but can't get it compiled in windows; I'm using mingw g++. I've changed all the include paths to windows paths and the jni.h is being located at compile time but not the jvm.dll.

undefined reference to `JNI_CreateJavaVM' linux

Here is the commands I've tried using to compile:

g++ -g -I"C:\Program Files (x86)\Java\jdk1.7.0_21\include" -I"C:\Program Files (x86)\Java\jdk1.7.0_21\include\win32" -L"C:\Program Files (x86)\Java\jdk1.7.0_21\jre\bin\server" callJava.cpp -ljvm

and...

**same as above with the additional** : -L"C:\Program Files (x86)\Java\jdk1.7.0_21\lib"

The error I get is:

undefined reference to `_imp__JNI_CreateJavaVM@12'

and the cpp being compiled:

#include <jni.h>

int main(){

    //firstTest();
    JavaVM *jvm;
    JNIEnv *env;

    JavaVMInitArgs vm_args;
    JavaVMOption options[1];
    options[0].optionString = "-Djava.class.path=C:/Users/Ron/Dropbox/jni/simple/ctojava/win";
    vm_args.version = JNI_VERSION_1_6;
    vm_args.options = options;
    vm_args.nOptions = 1;
    vm_args.ignoreUnrecognized = JNI_FALSE;

    int res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);

    jclass cls = env->FindClass("Hello");
    jmethodID mid = env->GetStaticMethodID(cls, "staticInt", "(I)I");
    env->CallStaticVoidMethod(cls, mid,10);

    jvm->DestroyJavaVM();
}

I've looked at many examples but still can't find a solution. Any help is appreciated!

UPDATE: I am pretty sure the the jvm.dll is being located because if I remove the -L"path_to_jvm" then I get the error:

mingw32/bin/ld.exe: cannot find -ljvm

Like I said, this exact approach works in linux, What else am I missing for windows?

like image 201
RBI Avatar asked Jun 05 '13 02:06

RBI


1 Answers

The issue you ran into can briefly be summed up as a name decoration problem. The linker couldn't find the function with the given name because it's decorated differently in the jvm.dll.

Looking at the initial error you got:

undefined reference to '_imp__JNI_CreateJavaVM@12'

it's hinting at two things:

  1. The @12 suffix at the end indicates that JNI_CreateJavaVM supposely uses the stdcall convention.
  2. The _imp_ prefix indicates that this function is from an import library that redirects to an externally loaded dll that has this function visible in its export table.

The function prototype in jni.h:

_JNI_IMPORT_OR_EXPORT_ 
jint JNICALL JNI_CreateJavaVM(JavaVM **, void **, void *);

probably looks like this after preprocessing:

__declspec(dllimport) jint __stdcall
JNI_CreateJavaVM(JavaVM **, void **, void *);

Now the gnu linker that comes with mingw can work with symbols from .a, msvc's COFF format .lib and .dll directly. In your original command, it only found jvm.dll in the search path provided (-L ...) so it tried to use that.

The problem is that in jvm.dll's export table the JNI_CreateJavaVM function is un-decorated so it looks like a cdecl function. This name doesn't match what the linker expects so you get the undefined reference error.

From looking at the Java Dev Kit, it includes an import library at jdk1.7.0_21\lib\jvm.lib which has the proper name decoration for this symbol. Your revised command works because by adding -L jdk1.7.0_21\lib to the search path it's now linking against jvm.lib and not jvm.dll.

like image 173
greatwolf Avatar answered Sep 23 '22 12:09

greatwolf