How to call Java functions from C++?
Asked Answered
N

4

49

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

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

Neon answered 4/5, 2009 at 10:3 Comment(0)
E
25

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.

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.

Embellish answered 4/5, 2009 at 10:40 Comment(3)
this type of invocation is what im looking for . the question is what is the con's of this methodNeon
complexity. but I believe, without 3rd party tools, the only method from what I have read.Gab
RE: "If the JVM already exists; e.g. your C program is invoked by the Java program (callback situation), you can cache the JNIEnv* pointer." Each thread has its own JNIEnv, do not share between threads.Tatiania
A
19

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

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 from Java (I understand this may well not be possible for your particular application, however)

Avron answered 4/5, 2009 at 10:13 Comment(2)
This isn't your fault, but the JNI Infocation interface link is broken. It seems Oracle moved every JNI resource they had, because broken links are all over the place when looking for JNI information.Rintoul
Looks like that link has been fixed nowAvron
T
6

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

Suppose you have a Java class:

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:

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 lot of 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.

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*.

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 must handle exceptions, and you must detach threads or things will get ugly.

Tatiania answered 14/3, 2013 at 19:21 Comment(3)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Ideational
Where do you get env from?Callow
That's the "details omitted part" ;-) Basically, you create a JVM, then each thread must have an Env. I added a little snippet. You also have to detach native threads from the JVM before they exit, or you will get nasty errors.Tatiania
C
-5

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

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").

It is easy and straight forward.

Chatter answered 29/7, 2016 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.