The information in this article applies to:
The GCDJava.exe sample illustrates how to use native methods from a Java application. This sample contains two Java classes: NativeM and Main. In the NativeM class, you define a native method called ComputeGCD. In the Main class, you define the main function in which you instantiate an object of type NativeM to call the ComputeGCD method. The ComputeGCD method in the NativeM class is actually implemented in C language, and it calculates the greatest common factor (denominator) of two integers.
The following file is available for download from the Microsoft® Software Library:
Gcdjava.exe
(http://support.microsoft.com/download/support/mslfiles/Gcdjava.exe .)
The GCDJAVA sample contains two parts: A Java project and a DLL project.
Following are the files included in the projects.
This project is named Native and it includes the following:
To use native methods in Java, you need msjavah. Msjavah is a tool that is installed by the Microsoft SDK for Java, and is available at http://www.microsoft.com/java . This tool helps you generate a header file from a Java class file. The header file generated by msjavah provides a function declaration of the native method. In this sample, it is the ComputeGCD method defined in the NativeM Java class. This header file can then be used in the native language project. In this sample, it is the DLL project in C.
The following steps give a short tutorial on how to use native methods in Java:
Msjavah.exe is installed in the %SDKDIR%\Bin directory. It helps create a header file from the Java class (NativeM.java) you created in step 2. The header file defines a structure that represents the Java class on the native language side and provides a function declaration for the implementation of the native method defined in that class.
Look at the header file NativeM.h to see what is added to this file. Notice that the Native.h is included in NativeM.h. Native.h is one of the header files from the Microsoft SDK for Java. It is located in the SDKJava\Include directory. You need to add this directory to your Microsoft® Developer Studio® (MSDEV) environment. See step 9 below.
Note If you prefer to export functions using __declspect(dllexport) in your source code, you do not need the .def file.
If the following exception appears, then you have a library path set in your environment, but the name of the directory where your library lives is not in the environment:
java.lang.UnsatisfiedLinkError
Or, if you get the following exception when trying to execute this sample under Microsoft® virtual machine (Microsoft VM) build 2252, or 2339, or later:
Error: java,lang.UnsatisfiedLinkError: native.dll cannot load because RNIGetCompatibleVersion export not found (new behavior). Press any key to continue.
This exception occurs because several enhancements have been made to the Raw Native Interface in the Microsoft SDK for Java versions 2.0x, which requires many RNI DLLs, designed for earlier versions of the Microsoft VM to be recompiled. To identify whether an RNI DLL is compatible with the current version of the Microsoft VM, it will call the RNI DLLs RNIGetCompatibleVersion export. RNI DLLs that do not export this API will not be loaded by the current Microsoft VM.
To fix this exception, make sure you export RNIGetCompatibleVersion in the Native.def file in the DLL project. Also include its implementation in the NativeM.c file in the same DLL project:
DWORD __cdecl RNIGetCompatibleVersion() { return RNIVER; /* RNIVER is defined in Native.h */ }
Rebuild the DLL. Copy the resulting DLL to the Windows\System directory.
Note It is also possible to access native code by creating COM objects that can be accessed via Java. There are several advantages to taking this approach:
For the latest Knowledge Base articles and other support information on Visual J++ and the Microsoft SDK for Java, see the following pages on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/
http://support.microsoft.com/support/java/
Native Code SDK