JNI, Java to C++ in Eclipse: undefined reference to '_imp__JNI_CreateJavaVM@12'

强颜欢笑 提交于 2019-12-11 14:14:29

问题


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


回答1:


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.


来源:https://stackoverflow.com/questions/17070879/jni-java-to-c-in-eclipse-undefined-reference-to-imp-jni-createjavavm12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!