[JNI_CreateJavaVM在64位C中失败

问题描述 投票:4回答:1

我有一个与JNI_CreateJavaVM exit code -1?类似的问题,除了我的代码在32位编译时运行,而在64位编译时运行失败。我正在使用Windows SDK 7.1平台更改Visual Studio Express 2010中每个配置的JDK位置。我的包括:

C:\Program Files\Java\jdk1.6.0_29\include; (for jni.h)
C:\Program Files\Java\jdk1.6.0_29\include\win32; (for jni_md.h)

和我的其他库是:

C:\Program Files\Java\jdk1.6.0_29\lib; (for jvm.lib and jawt.lib)

我的源代码只是试图在C中初始化JVM,然后无限循环以确保命令提示符在VSC ++中保持打开状态。

#include "stdafx.h"
#include "jni.h"

JNIEnv* create_vm(JavaVM ** jvm);

int i;
JavaVM* jvm;
JNIEnv * env;

int _tmain(int argc, _TCHAR* argv[])
{
    printf("Hello World!");
    env = create_vm(&jvm);

    if (env == 0) { return 7; }

    i = 0;

    while (1) { i++; }

    return 0;
}

JNIEnv* create_vm(JavaVM ** jvm) {

    JavaVMInitArgs vm_args;
    int ret;
    JavaVMOption options;

    //Path to the java source code
    options.optionString = "-Djava.class.path=H:\\jarpath\\jarfile.jar";
    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;

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

    if(ret < 0)
        printf("\nUnable to Launch JVM\n");
    return env;
}

同样,这适用于32位配置,唯一的区别是JDK根目录是32位文件夹(即Program Files(x86))。

打印后,

Error occurred during initialization of VM
Unable to load native library: Can't find dependent libraries

代码在VSC ++控制台窗口中退出并出现以下错误:

The program '[5684] JNI_Test.exe: Native' has exited with code 1 (0x1).

在库或基本C编码方面我缺少什么吗?我有一段时间没有用C编写代码了,而且我对整个库的链接不是很熟悉。

java c linker java-native-interface libraries
1个回答
4
投票

尝试此代码(它不使用导出的.lib文件,而仅需要JNI头和JVM.dll)。我已经使用了命令

gcc -m64 -o test.exe small_test.cpp -I "C:\Program Files\Java\jdk1.6.0_25\include" -I "C:\Program Files\Java\jdk1.6.0_25\include\win32" -lstdc++

进行编译(MinGW,但VC ++也应工作。Mingw使用较旧的MSVCRT)

检查GetLastError()。我怀疑64位C运行时有问题。

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

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

typedef /*_JNI_IMPORT_OR_EXPORT_*/ jint (JNICALL *JNI_CreateJavaVM_func)(JavaVM **pvm, void **penv, void *args);

JNI_CreateJavaVM_func JNI_CreateJavaVM_ptr;

JNIEnv* create_vm(JavaVM ** jvm)
{
    JNIEnv *env;
    JavaVMInitArgs vm_args;
    JavaVMOption options;
    HMODULE jvm_dll;
    int ret;

    options.optionString = "-Djava.class.path=D:\\monotest\\src_CJNIJava\\bin"; //Path to the java source code
    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;

    jvm_dll = LoadLibrary("C:\\Program Files\\Java\\jre6\\bin\\server\\jvm.dll");

    /// You might check the GetLastError() here after the LoadLibrary()
    if(jvm_dll == NULL) { printf("can't load dll\n"); exit(1); }

    JNI_CreateJavaVM_ptr = (JNI_CreateJavaVM_func)GetProcAddress(jvm_dll, "JNI_CreateJavaVM");

    /// You might check the GetLastError() here
    if(JNI_CreateJavaVM_ptr == NULL) { printf("can't load function\n"); exit(1); }

    ret = JNI_CreateJavaVM_ptr(jvm, (void**)&env, &vm_args);
    if(ret < 0) { printf("\nUnable to Launch JVM\n"); }
    return env;
}

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.