Microsoft Knowledge Base Email Alertz

KBAlertz.com: (222092) - This article details the process of calling native C/C++ code (in a DLL) from Java using JNI (Java Native Interface). This article presumes a working knowledge of the Visual C++ command-line compiler, CL.EXE. Make sure that you are using a version of...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **


Community Site



We Send hundreds of thousands of emails using ASP.NET Email


ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 222092 - Last Review: May 10, 2007 - Revision: 3.2

How To Call Native (DLL) Code From Java Using JNI

This article was previously published under Q222092

SUMMARY

This article details the process of calling native C/C++ code (in a DLL) from Java using JNI (Java Native Interface). This article presumes a working knowledge of the Visual C++ command-line compiler, CL.EXE.
  • Make sure that you are using a version of Microsoft's SDK for Java that supports JNI. JNI support was added in December of 1998 to SDK version 3.1 and above.
  • Make sure that the environment variable, CLASSPATH, contains a reference to "[WINDIR]\Java\Classes\Classes.zip" and "C:" (assuming that C: is your development drive).
  • Make sure that your "[SDK-Java]\Bin" directory is included in your path (for JavaH, JVC, and JView).
  • Make sure that Visual C++ is properly set up for command-line use. See your Visual C++ documentation for details.
  • Write your Java code:
    public class TestJNI {
       public native void greetings();
    
       static {
          System.loadLibrary("greet");
       }
    
       public static void main(String args[]) {
          new TestJNI().greetings();
       }
    }
    					
  • Compile the Java file:
    jvc TestJNI.java
    					
  • Run JavaH on the generated class file:
    javah -jni TestJNI
    					
  • Write the C/C++ code based on the generated header file:
    #include "TestJNI.h"
    #include <stdio.h>
    
    JNIEXPORT void JNICALL Java_TestJNI_greetings(JNIEnv *env,jobject jobj) {
       printf("Hello from Visual C++!");
    }
    					
  • Compile the C/C++ code:
    cl greet.cpp -Ic:\sdk-java.31\include -Fegreet.dll -MD -LD
    					
  • Test the application:
    jview TestJNI
    					

MORE INFORMATION

Things to note:
  • In the call to System.loadLibrary( ), omit the ".dll" extension from the native library's filename.
  • To support legacy native code, just have your DLL's function redirect the call to the legacy code.
(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Joseph B. Hall, Microsoft Corporation.

REFERENCES

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:
http://www.microsoft.com/java (http://www.microsoft.com/java)

APPLIES TO
  • Microsoft Java Virtual Machine
  • Microsoft Visual J++ 6.0 Standard Edition
  • Microsoft Software Development Kit for Java 3.1
Keywords: 
kbfaq kbhowto kbjnative KB222092
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Ronald - ronald.grandia@hotmail.com Report As Irrelevant  
Written: 11/30/2009 3:57 AM
How would it look if you need to give all kinds of variables (short, long, char's) to the greetings procedure in C++ ?

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please