如何从 C++ 调用 Java 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/819536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:51:38  来源:igfitidea点击:

How to call Java functions from C++?

javac++

提问by user63898

How can I call Java functions from a C++ application?

如何从 C++ 应用程序调用 Java 函数?

I know about calling them from CMD (or similar techniques), but I would rather not use them.

我知道从 CMD(或类似技术)调用它们,但我宁愿不使用它们。

采纳答案by Daniel H.

As an example, check Creating a JVM from C. It shows a sample procedure to create a JVM and invoke a method. If the JVM already exists; e.g. your C program is invoked by the Java program (callback situation), you can cache the JNIEnv* pointer.

例如,检查从 C 创建 JVM。它显示了创建 JVM 和调用方法的示例过程。如果JVM已经存在;例如,您的 C 程序被 Java 程序调用(回调情况),您可以缓存 JNIEnv* 指针。

As an advice, be careful caching pointers to the JVM from C/C++, there are some semantics involved as to what you can cache and it could be invoked later on. For that, as Brian Agnew pointed out, check the JNI reference.

作为一个建议,请小心从 C/C++ 缓存指向 JVM 的指针,涉及到一些关于可以缓存的内容以及稍后可以调用的语义。为此,正如 Brian Agnew 所指出的,请查看 JNI 参考。

回答by Brian Agnew

Check out the JNI Invocation interface. This will allow you to embed a JVM within your C (or C++) application.

查看JNI 调用接口。这将允许您在 C(或 C++)应用程序中嵌入 JVM。

Note that various easier mechanisms exist to facilitate calling C/C++ from Java (e.g. JNA). It may be worth considering inverting your problem such that you can call fromJava (I understand this may well not be possible for your particular application, however)

请注意,存在各种更简单的机制来促进从 Java(例如JNA)调用 C/C++ 。可能值得考虑反转您的问题,以便您可以Java调用(我知道这对于您的特定应用程序来说很可能是不可能的,但是)

回答by Wheezil

This page is helpful: http://hildstrom.com/projects/jni/index.html

此页面很有帮助:http: //hildstrom.com/projects/jni/index.html

Suppose you have a Java class:

假设您有一个 Java 类:

package foo;
public class bar {
    public static int timesTen(int input){
        return input * 10;
    }
}

Once you have a JVM and JNIEnv* (details omitted...) you can invoke the Java method from C++ as follows:

一旦你有一个 JVM 和 JNIEnv*(细节省略...),你可以从 C++ 调用 Java 方法,如下所示:

jclass myClass = env->FindClass("foo.bar");
jmethodID mid = env->GetStaticMethodID(myClass, "timesTen", "(I)I");
jint hundred = env->CallStaticIntMethod(myClass, mid, (jint)10);

This glosses over a lotof detail, including exception handling, which if omitted will crash your JVM. For all the gory details search on "Java Native Interface" and follow the Oracle links.

这掩盖了很多细节,包括异常处理,如果省略这些细节会导致 JVM 崩溃。有关所有血腥详细信息,请搜索“Java Native Interface”并点击 Oracle 链接。

Since someone asked... here's how you get an Env*. Note that if the JVM called your native code, it will already have an Env*.

既然有人问......这就是你如何获得Env*。请注意,如果 JVM 调用您的本机代码,则它已经有一个 Env*。

JNIEnv* env(0);
jint rv = vm->GetEnv((void**)&env, JNI_VERSION_1_6);
if (rv == JNI_OK) {
    return env;
} else if (rv == JNI_EDETACHED) {
    // This happens if you created the thread, not the JVM
    rv = vm->AttachCurrentThread((void**)&env, 0);
    if (rv != JNI_OK) {
        // error
    }
} else {
    // error
}

I cannot stress enough that using JNI to call Java from C/C++ is tremendously tedious and error-prone. Errors are cryptic and low-level. You musthandle exceptions, and you mustdetach threads or things will get ugly.

使用 JNI 从 C/C++ 调用 Java 非常乏味且容易出错,这一点我再怎么强调也不为过。错误是隐秘的和低级的。您必须处理异常,并且必须分离线程,否则事情会变得丑陋。

回答by Aditya

Another simple way to call java methods from CPP is through batch file.

从 CPP 调用 java 方法的另一种简单方法是通过批处理文件。

system() 

Is the method to call exe or bat files from the CPP. Put your class with the java path and the classpath in the batch file and call that batch file from the CPP using system("batch-file-name.bat").

是从CPP调用exe或bat文件的方法。将您的类与 java 路径和类路径放在批处理文件中,并使用system("batch-file-name.bat").

It is easy and straight forward.

这很简单直接。