引用了未解析的外部符号 __imp__JNI_CreateJavaVM@12

Hor*_*Lee 4 c++ java java-native-interface

我想编写一个调用Java方法的C++程序。

我正在尝试从 C++ 调用 Java 函数。如此处所述

http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html

但我在调试时遇到此错误并且无法处理它。我正在使用 Visual studio 2012。这是我的代码 C++ 代码。

#include "stdafx.h"
#include <jni.h>       /* where everything is defined */

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
JavaVM *jvm;       /* denotes a Java VM */
JNIEnv *env;       /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=C:\\Users\\yv\\workspace\\JNI\\bin";    // my class is under this directory. 

vm_args.version = 0x00010006; 
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;

/* load and initialize a Java VM, return a JNI interface
 * pointer in env */
JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);  // I got err msg "cannot convert parameter 2 from 'JNIEnv **' to 'void **' " so added (void **) as described in some other sources    

delete options;
jvm->DestroyJavaVM();


return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的 comp 版本上安装的 Java 是 C:\Users\yv>JAVA -version java version "1.7.0_17" Java(TM) SE Runtime Environment (build 1.7.0_17-b02) Java HotSpot(TM) 64-Bit Server VM (构建 23.7-b01,混合模式)

和我的java代码以备不时之需。

public class jniClass {
/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Hello, World!");
    System.out.println("Arguments sent to this program:");
    if (args.length == 0) {
        System.out.println("(None)");
    } else {
        for (int i=0; i<args.length; i++) {
            System.out.print(args[i] + " ");
        }
        System.out.println();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Tar*_*riq 5

我解决了错误:

unresolved external symbol _imp_JNI_CreateJavaVM@12 referenced
Run Code Online (Sandbox Code Playgroud)

通过为应用程序配置安装正确版本的 JDK。我在 Windows 7(64 位)计算机上安装了 64 位版本的 JDK。但是,我的应用程序是 32 位的。我卸载了64位的JDK版本并安装了32位的。后来就没有出现链接错误了。