本文介绍了[链接器错误]对'_imp__JNI_CreateJavaVM @ 12'的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是JNI的新手,我一直在尝试编写一个简单的程序来从C ++调用Java代码.

我在从C创建JavaVM时遇到问题,并且收到链接器错误.我已经尝试过在dev C ++中更改GNU库文件(我正在使用Dev c ++编译器),但到目前为止还没有运气.
JDK版本是1.6.27.

任何人都可以帮助我指出错误或建议逐步方法来编写此程序吗?

这是我要编译的C代码文件.

Hi,

I am new to JNI and I have been trying to write a simple program to call Java code from C++.

I am facing problem in creating the JavaVM from C and am getting the Linker error. I have tried changing the GNU library files in dev C++ (I am using Dev c++ compiler) but no luck so far.
The JDK version is 1.6.27.

Can anybody help me and point out the error or recommend a step-by-step approach to write this program?

Here is the C code file I am trying to compile.

#include <jni.h>
#include <windows.h>

#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
#define USER_CLASSPATH "." /* where Prog.class is */

main() {
    JNIEnv *env;
    JavaVM *jvm;
    jint res;
    jclass cls;
    jmethodID mid;
    jstring jstr;
    jclass stringClass;
    jobjectArray args;

#ifdef JNI_VERSION_1_2
    JavaVMInitArgs vm_args;
    JavaVMOption options[1];
    options[0].optionString =
        "-Djava.class.path=" USER_CLASSPATH;
    vm_args.version = 0x00010002;
    vm_args.options = options;
    vm_args.nOptions = 1;
    vm_args.ignoreUnrecognized = JNI_TRUE;
    /* Create the Java VM */
    // this should contain the path to the JVM DLL file
    HINSTANCE hVM = LoadLibrary("C:\\Program Files\\Java\\jdk1.6.0_27\\jre6\\bin\\client\\jvm.dll");
if (hVM == NULL)
{
    DWORD dwe = GetLastError();
    return -1;
}
typedef jint
(CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);

fpCJV CreateJavaVM = (fpCJV)::GetProcAddress(hVM, "JNI_CreateJavaVM");
res = CreateJavaVM(&jvm, (void**)&env, &vm_args);
#else
    JDK1_1InitArgs vm_args;
    char classpath[1024];
    vm_args.version = 0x00010001;
    JNI_GetDefaultJavaVMInitArgs(&vm_args);
    /* Append USER_CLASSPATH to the default system class path */
    sprintf(classpath, "%s%c%s",
            vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
    vm_args.classpath = classpath;
    /* Create the Java VM */
    res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
#endif /* JNI_VERSION_1_2 */

    if (res < 0) {
        fprintf(stderr, "Can't create Java VM\n");
        exit(1);
    }
    cls = (*env)->FindClass(env, "Prog");
    if (cls == NULL) {
        goto destroy;
    }

    mid = (*env)->GetStaticMethodID(env, cls, "main",
                                    "([Ljava/lang/String;)V");
    if (mid == NULL) {
        goto destroy;
    }
    jstr = (*env)->NewStringUTF(env, " from C!");
    if (jstr == NULL) {
        goto destroy;
    }
    stringClass = (*env)->FindClass(env, "java/lang/String");
    args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
    if (args == NULL) {
        goto destroy;
    }
    (*env)->CallStaticVoidMethod(env, cls, mid, args);

destroy:
    if ((*env)->ExceptionOccurred(env)) {
        (*env)->ExceptionDescribe(env);
    }
    (*jvm)->DestroyJavaVM(jvm);
}


推荐答案



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




with

// this should contain the path to the JVM DLL file
HINSTANCE hVM = LoadLibrary("C:\\Program Files (x86)\\Java\\jre6\\bin\\client\\jvm.dll");
if (hVM == NULL)
{
    DWORD dwe = GetLastError();
    return -1;
}
typedef jint	(CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);
fpCJV CreateJavaVM = (fpCJV)::GetProcAddress(hVM, "JNI_CreateJavaVM");
res = CreateJavaVM(&jvm, (void**)&env, &vm_args);


它应该工作;至少在我的系统上是这样.如果时间允许,我将继续进行调查.


it should work; at least it does on my system. I will continue to investigate further as time permits.


这篇关于[链接器错误]对'_imp__JNI_CreateJavaVM @ 12'的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 21:04
查看更多