Click here to Skip to main content
13,730,342 members
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 12 Jan 2008
Licenced CPOL

How to Call Java Functions from C Using JNI

, 12 Jan 2008
Rate this:
Please Sign up or sign in to vote.
This article covers calling Java functions from C using JNI. It also covers passing/returning simple parameters, arrays, and structure arrays in Java functions.


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.


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:

  • 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. contains the 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: 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:

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 the 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 the HelloWorld class. These signatures can be used to obtain the method IDs. The result of the above command can be seen below:

D:\Java Src\TestStruct>javap -s -p HelloWorld
Compiled from ""
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 the 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 the javac command.
  • Compile the 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 the following in the CTest.cpp file:



instead of:



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


About the Author

Software Developer (Senior)
United Kingdom United Kingdom
Ahmad Qarshi is an IT Consultant and have good expertise in different development technologies. He loves to code in .NET, JavaFX, Delphi, XSLT/Schematron. Above all very keen to learn new stuff Smile | :)

You may also be interested in...


Comments and Discussions

GeneralRe: My application crashes while calling JNI_CreateJavaVM function Pin
Khalid Noor23-Feb-10 1:15
memberKhalid Noor23-Feb-10 1:15 
GeneralRe: My application crashes while calling JNI_CreateJavaVM function Pin
ajalilqarshi23-Feb-10 2:13
memberajalilqarshi23-Feb-10 2:13 
GeneralRe: My application crashes while calling JNI_CreateJavaVM function [modified] Pin
Khalid Noor23-Feb-10 19:27
memberKhalid Noor23-Feb-10 19:27 
GeneralRe: My application crashes while calling JNI_CreateJavaVM function Pin
8-Mar-11 0:20
member8-Mar-11 0:20 
GeneralRe: My application crashes while calling JNI_CreateJavaVM function Pin
pratikmota9-May-11 22:55
memberpratikmota9-May-11 22:55 
GeneralSOLVED: Error message: "Can't find dependent libraries" [modified] Pin
tsjakkaa29-Jan-10 2:25
membertsjakkaa29-Jan-10 2:25 
Generalhelp me to sort out the error [modified] Pin
shariqmohd26-Jan-10 17:06
membershariqmohd26-Jan-10 17:06 
GeneralRe: help me to sort out the error Pin
ajalilqarshi27-Jan-10 13:09
memberajalilqarshi27-Jan-10 13:09 

Add the C:\Java\jdk1.6.0_18\include and C:\Java\jdk1.6.0_18\include\win32 in the "Additional Include Directories" of your project properties under C/C++\General.

Also add the jvm.lib to the additional libraries.
QuestionNot working for me ...... pls help!!! Pin
kumarmukt20-Jan-10 3:20
memberkumarmukt20-Jan-10 3:20 
GeneralRE: How to Call Java functions from C Using JNI Pin
chinmaya.mishra961-Aug-09 6:46
memberchinmaya.mishra961-Aug-09 6:46 
GeneralRe: RE: How to Call Java functions from C Using JNI Pin
Mohammed Anees13-Aug-09 23:20
memberMohammed Anees13-Aug-09 23:20 
GeneralVS 2008 Pin
thusithagh5-Jul-09 8:47
memberthusithagh5-Jul-09 8:47 
GeneralMy vote of 2 Pin
thusithagh5-Jul-09 6:54
memberthusithagh5-Jul-09 6:54 
Questionjvm.dll not found Pin
gdaley9-Jun-09 17:34
membergdaley9-Jun-09 17:34 
AnswerRe: jvm.dll not found Pin
gdaley9-Jun-09 18:50
membergdaley9-Jun-09 18:50 
GeneralJNI_CreateJavaVM returning -3 Pin
rostronix14-May-09 15:34
memberrostronix14-May-09 15:34 
GeneralUnknown Error Pin
Ankush M Gupta18-Oct-08 4:47
memberAnkush M Gupta18-Oct-08 4:47 
QuestionRe: Unknown Error Pin
jakko00724-Feb-09 0:36
memberjakko00724-Feb-09 0:36 
AnswerRe: Unknown Error Pin
Damos25-Mar-09 7:44
memberDamos25-Mar-09 7:44 
GeneralRe: Unknown Error Pin
Jagdish Vasani12-Jun-09 4:16
memberJagdish Vasani12-Jun-09 4:16 
Generalundeclared identifier.. Pin
bujal1-Jul-08 20:50
memberbujal1-Jul-08 20:50 
GeneralRe: undeclared identifier.. Pin
ajalilqarshi30-Jul-08 0:22
memberajalilqarshi30-Jul-08 0:22 
Questionhow can i do webservice Pin
norazanita30-Jun-08 17:45
membernorazanita30-Jun-08 17:45 
QuestionHow can i create jvm in dll? Pin
16-Jun-08 23:08
member16-Jun-08 23:08 
AnswerRe: How can i create jvm in dll? Pin
ajalilqarshi30-Jul-08 0:32
memberajalilqarshi30-Jul-08 0:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.180920.1 | Last Updated 13 Jan 2008
Article Copyright 2008 by ajalilqarshi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid