7

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?

Community
  • 1
  • 1
RBI
  • 803
  • 2
  • 14
  • 25
  • It can be a problem to link to libraries built with MSVC from g++. See this interoperability post: http://www.mingw.org/wiki/MixingCompilers. Have you considered using Visual Studio instead? – maba Jun 05 '13 at 13:07
  • @maba- I have tried in visual studio as well but I get the same result. Do you know for a fact that the jvm.dll is built with MSVC? I would guess that it is not, but it's just a guess. – RBI Jun 05 '13 at 13:24
  • 1
    @RBI It is built with MSVC. The way the function names are mangled in the export table is a giveaway. – greatwolf Jun 05 '13 at 20:26

2 Answers2

4

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.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • 2
    Very well explained, Thanks for clarifying this for me. – RBI Jun 06 '13 at 00:23
  • Hello. I am linking to the jvm.lib file and it still gives me this error. Any idea? The command used is g++ "-LC:\\Program Files\\Java\\jdk1.8.0_112\\lib" -o voce_synth.exe synthesisTest.o -ljvm – Renato Jan 04 '17 at 13:07
  • 1
    @Renato the `-ljvm` switch appends a `lib*` prefix to the import file. IOW with `-ljvm` g++ looks for `libjvm.a`. Try `-l:jvm.lib` or just passing the filename with fully path as is without `-l`. – greatwolf Jan 04 '17 at 13:23
  • @greatwolf Still having the same problem with -l:jvm.lib. Two things.. I am using mingw64 and java 64 bit. But for some reason it gives me the "@12" suffix which supposedly is for 32 bit. – Renato Jan 04 '17 at 13:58
  • 1
    You might have to check the symbol table in the jvm.lib using something like `nm` or `objdump`. Make sure the unresolved names are in there. It's possible the mangling is slightly different under 64-bits. – greatwolf Jan 04 '17 at 14:24
  • @greatwolf Turns out I made a mistake during the mingw64 installation. I had installed the 32-bit version instead! Now I have another problem but i'm searching for a solution. Thanks!! :) – Renato Jan 04 '17 at 17:29
2

Got it! after reading the post below I was able to compile and run the sample with the additional link in the compile command in order to link the jvm.lib:

-L"C:\Program Files (x86)\Java\jdk1.7.0_21\lib"

Link: Linking JNI to visual studio 2008

I'm no c/c++ expert so if anyone wants to explain why this additional link is required when it isn't in linux I would be glad to accept your answer.

Community
  • 1
  • 1
RBI
  • 803
  • 2
  • 14
  • 25