|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article describes the methodology to use Java code in C/C++. I have also discussed not only the simple parameter passing and returning but complex data structures such as structures and structure arrays to Java functions as well. BackgroundFew days ago my Manager asked me to write a code in C/C++ that will use the Java classes in another project. It took me a lot of time to grab the information on how to call simple functions as well as passing/returning complex data types. I found many articles describing the C/C++ function calls in Java using JNI but a 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 codeThe 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 the CTest.cpp. Other three Java classes are simply used in place of structures in Java. As there is no structure concept in Java so we can use classes for that purposes. This is what other three .java file contains. HelloWorld.java contain following functions that will be called from C/C++ code. public static void main(String args[]) { } public static void TestCall(String szArg) { } public static int DisplayStruct(ControlDetail ctrlDetail) { } public static void DisplayStructArray(WorkOrder ArrWO[]) { } public static Object ReturnObjFunc() { } To call these functions from C/C++ first you need to load the JVM using the following function JNIEnv* create_vm(JavaVM ** jvm) {
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
//Path to the java source code
options.optionString = "-Djava.class.path=D:\\Java Src\\TestStruct";
vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0)
printf("\nUnable to Launch JVM\n");
return env;
}
Kindly note that to use this code you will have to modify the vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6 Modify it if you have another JDK version installed. To call a specific Java function from C you need to do the following
One important thing to be noted here is specifying the function signatures while obtaining the method IDs. To obtain the correct method signature you can use the following Java command.
It will display you the signature of each function in HelloWorld class. These signature you can use to obtain the Method IDs. The result of above command can be seen below: D:\Java Src\TestStruct>javap -s -p HelloWorld Compiled from "HelloWorld.java" public class HelloWorld extends java.lang.Object{ public HelloWorld(); Signature: ()V public static void main(java.lang.String[]); Signature: ([Ljava/lang/String;)V public static void TestCall(java.lang.String); Signature: (Ljava/lang/String;)V public static int DisplayStruct(ControlNEDetail); Signature: (LControlNEDetail;)I public static void DisplayStructArray(WorkOrder[]); Signature: ([LWorkOrder;)V public static java.lang.Object ReturnObjFunc(); Signature: ()Ljava/lang/Object; } Kindly note that while specifying the Method name in the PrerequisitesBefore traveling down a difficult path, it is important to understand basic concepts and to have various frameworks and tools installed on your computer.
How to RunTo use this code follow the instructions below: Compile the *.java files using Compile CTest.cpp file using any C++ compiler I used MSVC++6. Converting this code to pure C from C++The attached code is written in C++. To convert this code into pure C you will have to modify following things in the CTest.cpp file. Use (*env)->FunctionName(env,args,..); instead of env->FunctionName(args,..);
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||