从JNI调用main方法失败 [英] Calling main method from JNI fails

查看:108
本文介绍了从JNI调用main方法失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个C ++类,该类应该通过以下方式调用Main.main:

I created a C++ class that is supposed to call Main.main by following: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#wp9502.

我没有使它起作用,所以我遵循了:

I didn't get it to work so I followed: http://www.coderanch.com/t/525082/CPP/create-JVM-native-code-call

和:

imp_JNI_Crea> http://www.codeproject.com/Questions/263687/Linker-error-undefined-reference-to- imp _JNI_Crea

imp_JNI_Crea">http://www.codeproject.com/Questions/263687/Linker-error-undefined-reference-to-imp_JNI_Crea

没有一个有效.因此,我将代码改回了oracle的Invocation API文章所说的内容(第一个链接).

None of which worked. So I changed my code back to what the Invocation API article by oracle says (the first link).

我的C ++代码如下:

My C++ code looks like:

在JNI.hpp文件中:

In JNI.hpp file:

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

class Jvm
{
private:
    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs jvm_args;
    JavaVMOption* options;

public:
    Jvm();
};

在JNI.cpp文件中:

In JNI.cpp file:

Jvm::Jvm()
{
    options = new JavaVMOption[3];

    options[0].optionString = "-Djava.compiler=NONE";
    options[1].optionString = "-Djava.class.path=C:/Users/Brandon/Documents/NetBeansProjects/Loader/build/classes";
    options[2].optionString = "-verbose:class";

    jvm_args.version = JNI_VERSION_1_6;
    jvm_args.nOptions = 3;
    jvm_args.options = options;
    jvm_args.ignoreUnrecognized = false;

    //JNI_GetDefaultJavaVMInitArgs(&jvm_args);
    JNI_CreateJavaVM(&jvm, reinterpret_cast<void**>(&env), &jvm_args);

    jclass MainClass = env->FindClass("loader.Main");


    //Crashes on the next line:
    jmethodID MainMethod = env->GetStaticMethodID(MainClass, "main", "([Ljava/lang/String;)V");

    MessageBox(NULL, "", "", 0);

    Sleep(1000);

    jvm->DestroyJavaVM();
    delete[] options;
}

我的Java代码如下:

My java code looks like:

package loader;

public class Main {

    public static void main(String[] args) {
        //JavaProcess.exec(ClientApplet.class);
        System.out.println("Hello!");
    }
}

详细打印:

[Loaded loader.Main from file:/C:/Users/Brandon/Documents/NetBeansProjects/Loader/build/classes/]

Process returned -1073741571 (0xC00000FD)   execution time : 1.730 s
Press any key to continue.

我做错了什么?为什么无法调用该方法? 我加载的JNI.dll来自:C:\Program Files\Java\jdk1.7.0_21\jre\bin\server\jvm.dll,因为最新的Java 7u25没有bin\client\jvm.dll.

What am I doing wrong? Why does it fail to call the method? The JNI.dll that I loaded is from: C:\Program Files\Java\jdk1.7.0_21\jre\bin\server\jvm.dll because the latest Java 7u25 doesn't have a bin\client\jvm.dll.

我什至静态链接到jvm.lib:C:\Program Files\Java\jdk1.7.0_21\lib\jvm.lib.

I even statically linked to the jvm.lib: C:\Program Files\Java\jdk1.7.0_21\lib\jvm.lib.

推荐答案

jclass MainClass = env->FindClass("loader.Main");

这是错误的.就像在方法签名中一样,使用JNI函数时必须使用斜杠而不是点.

This is wrong. You have to use slashes instead of dots when using JNI functions, just like in method signatures.

正确的代码是:

jclass MainClass = env->FindClass("loader/Main");

这篇关于从JNI调用main方法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆