Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling a Java method from a C++ dll

Posted on 2004-08-04
6
Medium Priority
?
382 Views
Last Modified: 2013-11-23
Hi,
I have gone through a number of JNI tutorials, but none of them have good enough samples of how to call java Methods from a C or C++ dll. I am running Windows XP, with j2sdk 1.4.2_05 and VC++ 6.0. Can somebody please give me a good example of how to do this, with step by step instructions.
Thanks and best regards,
Advait
0
Comment
Question by:advait75
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 672 total points
ID: 11714006
0
 
LVL 3

Assisted Solution

by:TILL
TILL earned 664 total points
ID: 11714064
JNI is basically a nice way to include in you java application calls to routines written in C++, which is a OS dependent language. Once you make a call, you can pass a certain Java Object as a function argument. This object can be cached on the c++ dll, and later be used to perform reverse callings. There are a couple of JNI functions  that you have to use in order to perform this reverse function call. If you look into the url provided by girionis, you'll find a section called "Calling Java Methods". Here you get this short sample:

Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth)
{
    jclass cls = (*env)->GetObjectClass(env, obj);
    jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "(I)V");
    if (mid == 0) {
        return;
    }
    printf("In C, depth = %d, about to enter Java\n", depth);
    (*env)->CallVoidMethod(env, obj, mid, depth);
    printf("In C, depth = %d, back from Java\n", depth);
}


This samples shows only how to call a java method from a native method of the same object(Class). If you add a seccond parameter to the native method, which would be another object, you can call a method of that object, or you can store the method/object reference for later use.

If you need more directions with that, please feeel free to ask.
Regards,
TILL.
0
 

Assisted Solution

by:3v0luti0n
3v0luti0n earned 664 total points
ID: 11731248
To do this, you have to use the Invocation API, which is kind of like the other end of JNI.  TILL's right about how to call a JNI method, but doesn't explain where you get that pesky "env" variable in the first place.  You do this by creating a Java Virtual Machine, nabbing its environmnent, and then passing that as the "env" variable to call the functions just like mentioned above.  The URL girionis listed is good, but here's a more specific one for how to call methods (take particular note of the part on how to get method signatures):

http://java.sun.com/docs/books/tutorial/native1.1/implementing/method.html

Here's a a simple example of how to create the JVM (in C++):

void main(int argc, char* argv[])
{
    JNIEnv_ *env;
    JavaVM *jvm;
    JDK1_1InitArgs vm_args;
    jint res, ret = 0;

    jclass cls = NULL;
    jmethodID cid = NULL;
    jobject obj = NULL;

    JNI_GetDefaultJavaVMInitArgs(&vm_args);

    //Specifies JDK 1.4.
    vm_args.version = 0x00010004;

    //Create the Java VM
    res = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
    if (res < 0) {
        cout << "Can't create Java VM\n" << res;
            cin >> res;
        return;
    }

/*
 * Remainder of code here.
 */

    jvm->DestroyJavaVM();
}


I don't know much about manipulating the vm_args variable, but I know you can set system properties and other goodies, and I can find out more if you're curious.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question