![]() |
Languages »
C / C++ Language »
Howto
Intermediate
License: The Code Project Open License (CPOL)
How to Call Java functions from C Using JNIBy ajalilqarshiThis article covers the calling Java functions from C Using JNI. It also covers passing/returning simple parameters, arrays, structure arrays in Java functions |
C++ (VC6, VC7, VC7.1, VC8.0), C++/CLI, C, Windows (Win2K, WinXP, Win2003, Vista), Java, Win32, Dev, Design
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
This 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.
Few 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.
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 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 options.optionString variable. You will have to set the path of the Java code. I mean where the Java classes are placed. Currently it being set to D:\Java Src\TestStruct. You can modify accordingly. You will also need to modify the JDK version information in the above code as it is mentioned below:
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
FindClass(,,) method.GetStaticMethodID and GetMethodID function calls.CallStaticVoidMethod, CallStaticIntMethod and CallStaticObjectMethod.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.
javap -s -p HelloWorld
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 GetMethodID function if the Method is a constructor then its Method Name will be <init>.
Before traveling down a difficult path, it is important to understand basic concepts and to have various frameworks and tools installed on your computer.
To use this code follow the instructions below:
Compile the *.java files using javac command.
Compile CTest.cpp file using any C++ compiler I used MSVC++6.
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,..);
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 13 Jan 2008 Editor: |
Copyright 2008 by ajalilqarshi Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |