Click here to Skip to main content
Click here to Skip to main content
Go to top

Using VC++ components from Java program

, 13 Mar 2007
Rate this:
Please Sign up or sign in to vote.
This article describes how to access native windows dll from Java programs to take advantages of facilities provided by windows which are not possible to use directlly from Java. It introduces JNI ( Java native interface )

Sample Image - JNITest.jpg

Introduction

JNI is my favourite programming framework in Java; it gives you freedom to use natively created code. For example, if you want to use any Windows API (i.e. dll) from your Java code, you can use JNI.

The most popular advantage of Java is platform independence. However, sometimes this feature makes it very difficult to integrate Java programs with native platforms.

For example, in our project we are using MSM Q (Microsoft Messaging Que) for a queuing mechanism to maintain messages (to avoid loss of messages) before writing them to the database. Microsoft is providing MSM Q API which is a Windows' API. But our program produces messages is in Java, and Java can't directly access such VC++ API (dll). So we have decided to make an intermediate DLL which will be work as bridge between Java and VC++ (we have used the technology Java Native Interface, or JNI).

Before proceeding further, you should know / be familiar with some terms.

  • Native Methods: Native method is a method whose declaration would be in a .java file and the definition would be in your native code. (for Window's it is in VC++).
  • Static block : Static block in Java is the portion of code executed before anything else happens.

So let's start. First, write your Java program with a declaration of native methods. The following code is from the source sample:

public class JNITest 
{
     static 
       {
        System.loadLibrary("JNITest");  // Loading dll in memory 
       }
        
    native void showMessage( String str);  // Declaring native method
    public JNITest()
    {
        System.out.println( "In the constructor of the Java program" );        
    }
        
    public static void main( String s[] )
    {
         JNITest JNT = new JNITest();
         JNT.showMessage("Passing string from Java");  
    } 
}

In above code we have declared showMessage method which is native, and called it w.r.t. object of class JNITest.

Perform the following:

  1. Compile the code to generate .class file.
  2. In the JDK folder you will find an utility called javah.
  3. Go to the command prompt and execute javah -jni JNITest

Execution of the above will produce the file JNITest.h. This .h file contains VC++ equivalent names of native methods declared in our Java code

Creating JNI DLL

We are almost half-way done.

Now create a simple dll project in Visual Studio (or any other win32 IDE) and add above .h file to the project.

Add the path of your JRE\INCLUDE folder to your includes to get include file jni.h.

Now you will find the names of the methods declared in the .java file in somewhat different manner.

In the above case you will get:

JNIEXPORT void JNICALL Java_JNITest_showMessage(JNIEnv *, jobject, jstring);

Here, the third argument is your argument from Java program. You can convert it to the native form as:

const char *strS1 = env->GetStringUTFChars( s1, 0);

Here you can use strS1 in the program as a C++ string. In the demo program I have used it to display it in Messagebox. There are many methods of JNIEnv by which we can convert various types of data from Java to C++.

Releasing the string after use is compulsory, and is not automatic. JNI activities are presumed to be external to JVM, so it will not throw any kind of exception which could be caught in Java code. If you forget to release the string before leaving the function, this could result in the crashing of your JVM.

You can release the string using the function ReleaseStringUTFChars:

env->ReleaseStringUTFChars( s1, strS1);

The whole code would be look like this:

JNIEXPORT void JNICALL Java_JNITest_showMessage(
    JNIEnv * env, jobject job, jstring str )
{    
       const char *strMsgPtr     = env->GetStringUTFChars( str , 0);    
       //Converting string to C++ character pointer
      
       MessageBox( 0, strMsgPtr,"Message box from VC++ ", 0 ); 
       //Using the string 

       env->ReleaseStringUTFChars( str, strMsgPtr);  
       // Releasing the string ( character pointer )
}

Now you are free to do anything with Windows (or VC++) components - you can now access any Windows API.

Build the dll and place it in your Java program's folder (the folder which contains your .class file).

Now run your Java program.

Note: If you get UnSatisfiedLinkError, Check your method signature (prototype). It must be as same as provided in the .h file

Using JNI, you can access various facilities provided by Windows from Java programs.

License

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

Share

About the Author

PrafullaVedante
Software Developer Symantec Corporation
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberjyl_hrb17-Apr-11 21:46 
GeneralRe: My vote of 4 PinmemberPrafullaVedante26-May-12 6:18 
Questionwhat's this error!! [modified] Pinmemberlpbolg23-Nov-10 14:56 
AnswerRe: what's this error!! PinmemberPrafullaVedante6-Dec-10 21:57 
AnswerRe: what's this error!! PinmemberPrafullaVedante13-Jul-11 8:56 
Questionno dialog Pinmemberqzi2-Dec-09 1:19 
Questionhow to use .lib file in java Pinmembermanucng21-Sep-09 19:08 
GeneralTemplate wrappering PinmemberSachinVadvadgi15-May-09 0:56 
GeneralJNI PinmemberSachinVadvadgi15-May-09 0:35 
QuestionHow Can I use an ATL dll written in VC++6.0 in Java Swing Pinmembervahid_sabanet10-Feb-09 19:39 
QuestionHow to add path of JRE\INCLUDE folder to get include file jni.h Pinmemberjhgghg27-Jan-09 13:26 
Hi
 
Where to add the path of JRE\INCLUDE folder to get include file jni.h
 
I am stuck at this point. Could please let me know ASAP.
 
Thanks..
GeneralInstead of dll Can I use exe PinmemberNoman Ahmed16-Mar-07 21:32 
Generalyes! Good joob Pinmemberbruno leclerc14-Mar-07 5:17 
GeneralError while run cmd [java JNITest] PinmemberZeeshan Zahoor7-Jan-07 20:55 
GeneralRe: Error while run cmd [java JNITest] PinmemberPrafullaVedante9-Jan-07 18:24 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 13 Mar 2007
Article Copyright 2006 by PrafullaVedante
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid