How to call Java library from C++?


1.       Set up VisualC++ .NET for the include file, jni.h (from Tools->Option)

In the above example, we used $(JAVA_HOME) environment variable which indicates C:\Program Files\Java\jdk1.5.0_06 in my environment.

I added 2 lines as,

              $(JAVA_HOME) \include\win32


Once you’ve done this, you can include the header files for JNI in your C++ application.


2.       Set up VisualC++ .NET for the lib file, jvm.lib (from Tools->Option)

You need to add $(JAVA_HOME) \lib directory under “Library files” setting, thus VC++ .NET can refer to the target .lib file that we are going to add by the following step.


<Solution Explorer><Right Click><Properties> (<- this is so difficult to find!!)

We then adde“jvm.lib” library file which is stored under $(JAVA_HOME) \lib directory.

Once you’ve done this, you can link the application.


3.       Set up PATH environment variable to include jvm.dll file

Add the path that contains your jvm.dll from Control Panel.

This jvm.dll is used by the executable you have built.


4.       Prepare the Java program as .jar library

For this case, I have implemented the follwoing Java class, that simply shows the greetings on the standard output.

package com.aksys.inv;


public class MyInvocation {


       public void hello() {

              System.out.println("Hello from Java!");



       public static void main(String args[]) {

              MyInvocation inv = new MyInvocation();






5.       Call the library in the following manner


 * This #pragma unmanaged is required for .NET bug!

 * If this is not described, GedMethodID should return System.TypeLoadException,

 * a .NET defined exception.


#pragma unmanaged                                              


#include <stdio.h>



 * For JNI application, this is mandatory


#include <jni.h>


int main(int argc, char* argv[]) {

        printf("This is C++\n");


        try {

                JavaVM *jvm;       /* denotes a Java VM */

                JNIEnv *env;       /* pointer to native method interface */


                JavaVMOption options[2];

                // classpath

                options[0].optionString = (char *) "-Djava.class.path=" \


                options[1].optionString = (char *) "-Djava.compiler=NONE";


                JavaVMInitArgs vm_args; /* JDK VM initialization arguments */

                vm_args.version = JNI_VERSION_1_4;

                vm_args.options = options;

                vm_args.nOptions = 2;

                vm_args.ignoreUnrecognized = JNI_TRUE;


                /* Get the default initialization arguments and set the class path */


                JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);


                // CAUTION!!! hierarchy delimitter is not '.' but '/' !!!

                jclass cls = env->FindClass("com/aksys/inv/MyInvocation");     

                jobject obj = env->AllocObject(cls);


                 * NOTE: Signature can be parsed by "javap" console program.

                 * ex) F:\MyDocuments\Java\MyTrial\InvocationAPI\src>javap -classpath

                 * F:\\MyDocuments\\Java\\MyTrial\\InvocationAPI\\dest\\myinvocation.jar

                 * -s -public com.aksys.inv.MyInvocation


                jmethodID mid = env->GetMethodID(cls, "hello", "()V");         

                env->CallVoidMethod(obj, mid);



        } catch (...) {

                printf("Exception is caught.\n");

                return 1;



        return 0;













(#pragma unmanagedについて)