Introduction

This article describes the methodology to use Java code in C/C++. I have not only discussed simple parameter passing and returning, but complex data structures such as structures and structure arrays to Java functions as well.

Background

A few days ago, my manager asked me to write code in C/C++ that will use Java classes in other projects. It took me a lot of time to grab some information on how to call simple functions as well as pass/return complex data types. I found many articles describing C/C++ function calls in Java using JNI, but very few discussing calling Java functions in C/C++ using JNI. At that time, I decided to write an article so that other people could get help from it.

Using the code

The CTest.cpp file contains the code that calls different functions from Java classes. Java classes that are used in this project are given here under:

HelloWorld.java

ControlDetail.java

WorkOrder.java

ReturnData.java

HelloWorld.java contains the functions that will be called from CTest.cpp. The other three Java classes are simply used in place of structures in Java. As there is no structure concept in Java, we can use classes for that purpose. That is what the other three .java files contain.

HelloWorld.java contains the following functions that will be called from C/C++ code:

Kindly note that to use this code, you will have to modify the options.optionString variable. You will have to set the path of the Java code: where the Java classes are placed. Currently, it being set to D:\Java Src\TestStruct. You can modify it to you situation. You will also need to modify the JDK version information in the above code, as shown below:

I've running java classes which I need to call from c language. There is a supporting jar file and a supporting dll. I've observed that my java code in c lang runs upto a certain limit i.e. when I give System.load("path//to//dll//dll_name-(.dll)"). The jave class fails to run after this line. Please help me. P.S. I'm a complete newbie to jni

When i Compiled VC++ code I got An error "program can't start becouse jvm.dll is missing from your computer " Please tell me what should i do to resolve it .
java 6 and VC++ 2008 are already installed in MY pC.

The example provided by you is very useful and it has worked wonders for me . Now the problem is that if I am using a Java file with a package included in it but then the findclass function returns nothing so what changes should a make in the java file as its important for me to include the package.

I've compiled and built the program without errors, but the program quits when it gets to the line:

int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);

which is in the method:

JNIEnv* create_vm(JavaVM ** jvm)

There are no error messages of any kind, it just doesn't work. I've used some print statements which appear in the console box and break points to find where exactly it stops and I'm pretty sure it's that line. The Output tab shows this:

The memory leak can be easily seen if looping this snippet 1 sequence for a long long time, the NewStringUTF will return null after some times (after 2,700,000 loops on my system). This can also be seen under windows by looking at free memory melting constantly under task manager. When the workaround is used, this problem does not happens anymore...

The example gave me the missing link while searching for a way to use Java-classes/code from inside C/C++-programs. Thanks!

Now I'm still searching for a wrapper-generator that is able to generate the C++-Interface and mapping-code from a set of Java-classfiles.
The other way round (calling C/C++ from inside Java) 'swig' ist the tool to do this.
There are many more wrapper-generator besides swig but they all only wrap native code to Java.