c++ - 0xC0000005 : Access violation reading location 0x0000000000000000

标签 c++ jar jvm java-native-interface

我正在尝试使用 JNI 在 C++ 程序中使用 jar 文件。

我这样做了:

// TestJavaToCpp.cpp : Defines the entry point for the console application.
//

#include <jni.h>
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;


int main()
{
    JavaVM *jvm;
    JNIEnv *env;
    JavaVMInitArgs vm_args;
    JavaVMOption options;
    options.optionString = "-Djava.class.path=HelloWorld.jar";
    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 1;
    vm_args.options = &options;
    vm_args.ignoreUnrecognized = 0;
    int ret = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    if (ret == 0) {
        jclass cls = env->FindClass("HelloWorld");
        if (cls != 0) {
            jmethodID meth = env->GetStaticMethodID(cls, "Hello", "([Ljava/lang/String;)V");
            jarray args = env->NewObjectArray(0, env->FindClass("java/lang/String"), 0);
            env->CallStaticVoidMethod(cls, meth, args);
        }
    }
    return ret;
}

HelloWorld.jar 与 exe 文件位于同一文件夹中时。 但是,我不断收到错误

0xC0000005: Access violation reading location 0x0000000000000000

当它尝试创建 JVM 时。

编辑 1: 现在代码看起来像这样:

// TestJava.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <jni.h>
#include <Windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;

#define CLEAR(x) memset(&x, 0, sizeof(x))

int main()
{
    JavaVM *jvm(0);
    JNIEnv *env(0);
    JavaVMInitArgs vm_args;
    CLEAR(vm_args);
    JavaVMOption options;
    CLEAR(options);

    options.optionString = "-Djava.class.path=HelloWorld.jar";
    options.extraInfo = 0;
    vm_args.version = JNI_VERSION_1_8;
    vm_args.nOptions = 1;
    vm_args.options = &options;
    vm_args.ignoreUnrecognized = 0;
    int ret = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
        
    return 0;
}

我只留下了实际创建虚拟机的代码。当它起作用时,我会继续,

现在我注意到之前出现的错误,但我没有注意它们:

'TestJava.exe' (Win32): Loaded 'C:\Users\amitb\OneDrive\מסמכים\Visual Studio 2015\Projects\TestJava\Debug\TestJava.exe'. Symbols loaded.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\apphelp.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Program Files (x86)\Java\jdk1.8.0_91\jre\bin\client\jvm.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\wsock32.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\psapi.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmmbase.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr100.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ws2_32.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Program Files (x86)\Java\jdk1.8.0_91\jre\bin\verify.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Program Files (x86)\Java\jdk1.8.0_91\jre\bin\java.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Program Files (x86)\Java\jdk1.8.0_91\jre\bin\zip.dll'. Cannot find or open the PDB file.

Exception thrown at 0x025A0202 in TestJava.exe: 0xC0000005: Access violation reading location 0x00000000.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. Cannot find or open the PDB file. 'TestJava.exe' (Win32): Loaded

'C:\Windows\SysWOW64\windows.storage.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\SHCore.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\powrprof.dll'. Cannot find or open the PDB file.

'TestJava.exe' (Win32): Loaded 'C:\Windows\SysWOW64\profapi.dll'. Cannot find or open the PDB file.

The thread 0x2760 has exited with code 0 (0x0).

The thread 0x494c has exited with code 0 (0x0).

The thread 0x3368 has exited with code 0 (0x0).

The thread 0x2e48 has exited with code 0 (0x0).

The thread 0x3d5c has exited with code 0 (0x0).

The thread 0x3b2c has exited with code 0 (0x0).

The thread 0x4490 has exited with code 0 (0x0).

The thread 0x3bb4 has exited with code 0 (0x0). The thread 0x4d18 has exited with code 0 (0x0).

The thread 0x2cac has exited with code 0 (0x0).

The thread 0x4ec8 has exited with code 0 (0x0).

The program '[18476] TestJava.exe' has exited with code 0 (0x0).

看粗体字。这是抛出异常的地方......

最佳答案

你需要设置options.extraInfo = 0;

我会做好所有这些。防御性编程要求初始化所有内容。

#define CLEAR(x) memset(&x, 0, sizeof(x))
JavaVM *jvm(0);
JNIEnv *env(0);
JavaVMInitArgs vm_args;
CLEAR(vm_args);
JavaVMOption options;
CLEAR(options);

注意:

typedef struct JavaVMOption {
    char *optionString;
    void *extraInfo;
} JavaVMOption;

编辑:这些都不是问题所在。解决方案在评论中——JVM 正在抛出访问冲突异常。它们会被捕获并处理,但不会在 Visual Studio 让您相信存在问题之前发生。

关于c++ - 0xC0000005 : Access violation reading location 0x0000000000000000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37651402/

相关文章:

java - 列出当前 JAR 文件目录中的文件

java - 如何在 Maven 中包含加载其他 jar 的 jar?

java - Eclipse 导出工具与命令行 jar 工具

Java类动态加载过程

java - 高 XMX:java.lang.outofmemory native 内存分配 (malloc) 无法为 ChunkPool::allocate 分配 32756 字节

java - 您可以覆盖由 -XX+HeapDumpOnOutOfMemoryError 生成的堆转储的文件权限吗?

c++ - 使用 SDL TTF for SDL 2 看不到我的文本

c++ - C++中指针的类型转换和类型转换

c++ - 有异常的 Windows 并发运行时任务调度

c++ - 使用 'delete' 运算符清理内存时出现“调试断言失败”错误