How to Call Java functions from C Using JNI

 

转自: http://www.codeproject.com/KB/cpp/CJniJava.aspx

标题: How to Call Java functions from C Using JNI

作者: ajalilqarshi

 

文章内容

-------------------------------------------------------------------

By This article covers the calling Java functions from C Using JNI. It also covers passing/returning simple parameters, arrays, structure arrays in Java functions

    * Download src_CJNIJava - 26.14 KB

Introduction

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

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.
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:
Collapse

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

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

Collapse

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:

Collapse

    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

   1. Obtain the Class reference using the FindClass(,,) method.
   2. Obtain the Method IDs of the functions of the class that you want to call using GetStaticMethodID and GetMethodID function calls.
   3. Call the functions using 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:
Collapse

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

Before traveling down a difficult path, it is important to understand basic concepts and to have various frameworks and tools installed on your computer.

   1. You will need the Sun Java Developer Kit (JDK). I recommend Java 1.6.0.
   2. Any C/C++ compiler installed.

How to Run

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

    (*env)->FunctionName(env,args,..);

instead of
Collapse

    env->FunctionName(args,..);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author
ajalilqarshi

Software Developer (Senior)
Assured Information Systems
United Kingdom United Kingdom

Ahmad Jalil Qarshi is a Software Engineer in Assured Information Systems (a Software solution provider to Pharmaceutical industry, FDA USA and EMEA) in different technologies like .NET, Delphi 2007, XSLT/Schematron.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值