0

I'm a beginner to c++ and JNI and I want to call a Java methode from my C++ program. When compiling (with Eclipse) I get the following error:

undefined reference to '_imp__JNI_CreateJavaVM@12'

I searched for this problem and came across this post

There the answer was, if I get that right, including the jvm library to the compiling command. Since I'm not compiling by hand I'm not sure how I can make Eclipse do this. Could somebody explain that step by step for a complete beginner?

Here is the code, in case the compiling command won't change anything and the code has some errors. In this part the error is displayed when calling JNI_CreateJavaVM:

JNIEnv* create_vm(JavaVM ** jvm) {

JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;

/*
 * The following is a little messy, but options.optionString wants a char* not a string
 * So I convert the String-path to a char-array
 */
string stringPath = "-Djava.class.path=C:\\Users\\Chris\\workspacejava\\PP\\src\\Tests"; //Path to the java source code

int sLen = stringPath.length();

char javaPath [sLen + 1];
int i = 0;
for(; i < sLen; i++)
{

    javaPath[i] = stringPath[i];
}
javaPath[i] = '\0';

options.optionString = javaPath;
vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;


int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args); //This call results in the error: undefined reference to '_imp__JNI_CreateJavaVM@12'
if(ret < 0)
    printf("\nUnable to Launch JVM\n");     
return env;}

And here I call this function:

    int main(int argc, char* argv[])
{
    JNIEnv *env;
    JavaVM * jvm;
    env = create_vm(&jvm);
    if (env == NULL)
        return 1;
    ...
    int n = jvm->DestroyJavaVM();
    return 0;
}

Further Informations: (I don't know if they help) I use Windows 7. Both Eclipse and JDK are 64Bit. I'm using MinGW GCC to compile my code.

I'm glad for every piece of advice

Community
  • 1
  • 1
user2479025
  • 11
  • 1
  • 4

1 Answers1

0

That's right, you need to add the jvm library to your build.

  1. Open the project properties dialog.
  2. Select C++ Builder > Settings on the left.
  3. Select the Tool Setting tab.
  4. Select MinGW C++ Linker > Libraries.
  5. Add jvm in the Libraries section.
  6. Add the path to your JDK lib folder in the Libraries Search Path section.

I have a JAVA_HOME environment variable pointing to a JRE subfolder of a JDK so I enter the search path like this: "${env_var:JAVA_HOME}/../lib", with quotes because the environment variable expands to a path with spaces in it.

Now, you might have 32-bit and/or 64-bit JDKs installed. Be sure to use files from the same bit-ness as your compiler output. That includes running your exe with the right jvm.dll first on the DLL search path. On 64-bit Windows, the 32-bit JDK is usually installed under C:\Program Files (x86)\Java....

The @12 says that your compiler is producing 32-bit code (the arguments to JNI_CreateJavaVM are 3 pointers of 4-bytes each) so the linker needs the 32-bit version of jvm.lib rather than the 64-bit version.


BTW—Your java.class.path looks a suspicious for two reasons:

  1. Tests looks like the name of a class. The class path should be a ;-separated list of paths to the root folder of packages. If Tests is a class in the default package, just lop off Tests from your class path.
  2. Eclipse usually sets up Java projects with separate source and binary folders. The paths in the class path are where java should search for .class files. If your project is configured to compile .java files from the src folder to .class files in the bin folder, then replace src with bin in your class path.
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • Thank you so much for your help. But I still get different errors for the different things I tried: 1. I tried including the absolute path of the lib in the Librarie Search Path ("C:\Program Files\Java\jdk1.7.0_21\lib"). I got the same error as before. But I saw that the jvm.dll file isn't in the lib folder. – user2479025 Jun 14 '13 at 21:16
  • 2. I included the absolute path of the jvm.dll file ("C:\Program Files\Java\jdk1.7.0_21\jre\bin\server"). This resulted in the error: C:\Program Files\Java\jdk1.7.0_21\jre\bin\server/jvm.dll: file not recognized: File format not recognized. I also tried it with "/" instead of "\" in the path, but I got the same error. 3. I tried it with the JAVA_HOME path ("${env_var:JAVA_HOME}/bin/client") then I get the error: c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -ljvm. I am not sure how to continue now. – user2479025 Jun 14 '13 at 21:16
  • Note: I found 3 different JDK-folders that all ahve a jvm.dll file ("Program Files\Java\jdk1.7.0_21", "Program Files\Java\jre7", "C:\Program Files (x86)\Java\jre7") Does it make a difference which one of them I try to include? – user2479025 Jun 14 '13 at 21:19
  • jvm.dll isn't involved until runtime. The linker needs to know paths for .lib files and be told to look in jvm.lib for functions to be linked. – Tom Blodget Jun 15 '13 at 02:09
  • When I give Eclipse the path to the jvm.lib file ("C:\Program Files\Java\jdk1.7.0_21\lib") I get the same error I started with : C:\Users\Chris\workspacec++\JavaWrap\Debug/../CTest.cpp:65: undefined reference to `_imp__JNI_CreateJavaVM@12. What did I do wrong? Is it possible that I missed out something I should have done earlier? Something like including the jvm.lib-Path to the Path environment variable or something similar (I tried this one already). I'm sorry if I tax your patience with stupid errors but I have really no idea how to continue... – user2479025 Jun 15 '13 at 15:26
  • @user2479025 I am having the same problem; switching from dll to library still produces the same error (undefined reference). How did you get it to finally work? – Loduwijk Oct 13 '16 at 20:45
  • I'm having the same problem as well. Do you remember what you did @user2479025 ? – Renato Jan 04 '17 at 13:12