Java Calling Native Functions in .DLL on Windows

How to call a function in .dll from Java on Windows is introduced in this post with an example.

Platforms used:
OS: Microsoft Windows XP [5.1.2600]

C to .dll compiler: MS Visual Studio 2008

JDK:

java -version
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

1. Write the Java code

As follows.

public class JNITest {
public static void main (String args[]) {
  if (args.length > 0) {
    int x = new Integer(args[0]).intValue();
    int a = new MyNative().cubecal(x);
    System.out.println(a);
  }
}
}

class MyNative {
public native int cubecal(int x);
static {
  System.loadLibrary("mynative");
}
}

2. Generate the MyNative.h for call native functions

javac JNITest.java

javah -jni MyNative

3. The native code

Note: the file name should be an .c file.

According to MyNative.h:

#include "jni.h"

int cube(int x) {
  return (x*x*x);
}

JNIEXPORT jint JNICALL Java_mynative_cubecal
(JNIEnv *e, jobject o, jint x) {
  return cube(x);
}

4. Configure dependencies in MS visual studio

Put ‘$JAVA_HOME$/include/jni.h’ ‘$JAVA_HOME$/include/win32/jni_md.h’
and ‘MyNative.h’ to the project directory in VS2008

5. Build mynative.dll

Just follow MS Visual Studio’s method.

6. Copy mynative.dll to the directory where the Java project is in.

The directory is like this:

2008-06-02  14:52               413 mynative.class
2008-06-02  14:52               719 JNITest.class
2008-06-02  15:39             7,168 mynative.dll

7. Run the Java program:

java JNITest 9

729

The cubical computation is done by the function from the C library correctly.

Similar Posts

  • Setting MATE as Default Desktop Environment for startx on Fedora

    I ever discussed Starting KDE from Command Line by startx for KDE and GNOME. Recently, I installed MATE desktop on Fedora 17 and am very happy with it—dear, Gnome 2 is back. However, I checked the /etc/X11/xinit/Xclients script which is called by ‘startx’ and it only includes ‘startkde’ and ‘gnome-session’ without support to MATE. But…

  • How to export and import message filters of Thunderbird

    I have several installation of Thunderbird under Linux to check/send emails using IMAP email servers. I use filters extensively to manage my emails. How to export and import message filters of Thunderbird so that I can synchronize the rules on several installations? About message filters in Thunderbird (More at http://kb.mozillazine.org/Filters_(Thunderbird)): Filters are account specific, there…

  • MFC程序使用系统风格界面

    VC6默认编译出来的程序在XP下Luma风格下运行也是Windows的经典界面, 有损界面的美观与统一. VC2008默认设置下如果不是使用的unicode也是如此. 本文给出使VC6和VC2008可以编译出使用系统界面风格的解决方案. 1. 使VC6编译出使用系统风格的程序 步骤如下: 1) 创建一个.manifest文件的资源. 在res/文件夹下创建一个跟以程序名加.manifest的文件, 如果程序为test.exe, 则创建test.exe.manifest 文件可由此下载: https://www.systutorials.com/t/g/programming/resultcollector.manifest/ 注意要使用utf-8编码保存。 2) 将新定义的资源加入到.rc2文件中, 类型设为24. 打开res/文件夹下的.rc2文件, 在其中加入如下定义: 1 24 MOVEABLE PURE “res/test.exe.manifest” 其中的文件地址按1)步中修改的设置即可. 之后编译即可, 为了使程序界面可能充分利用系统的界面特性, 可以将界面字体设置为TrueType类型的, 利用Windows XP等系统的屏幕字体平滑特性. 2. 使VC2008编译出使用系统风格的程序 在VC2008下就比较简单了, 如果程序字符集使用unicode则默认就是使用系统界面风格的, 如果选择其它的类型, 则编辑下stdafx.h即可. 最后面部分找到这么一段: #ifdef _UNICODE #if defined _M_IX86 #pragma comment(linker,”/manifestdependency:”type=’win32′ name=’Microsoft.Windows.Common-Controls’ version=’6.0.0.0′ processorArchitecture=’x86′ publicKeyToken=’6595b64144ccf1df’ language=’*'””) #elif defined _M_IA64 #pragma comment(linker,”/manifestdependency:”type=’win32’…

Leave a Reply

Your email address will not be published. Required fields are marked *