Custom Search

Wednesday, November 4, 2009

JNI (Java Native Interface) - Generating a DLL using Turbo C/C++ Compiler

This tutorial shows you how to write a JNI (Java Native Interface) program using Turbo C++ Compiler and JDK1.5 on a Windows XP machine.

Why Turbo C++ Compiler

Although Turbo C compiler is very old and there are many good C/C++ compiler available, like "Microsoft Visual C++" and other free compilers

This tutorial is basically to show that it’s possible to generate a windows System library (DLL) using Turbo C++ and which can be called by the JNI programs using it.

Borland Turbo C++ is the first C compiler I used to compile C programs but was always curious to know if JNI programming is possible with this compiler.

So here is a step-by-step process to write a small “Hello World” application using Turbo C and JDK1.5

Step-1: Download Turbo C and JDK.

This tutorial uses Turbo C++ 5.5 and JDK 1.5

Download links

Borland C++ Compiler 5.5

Sun JDK 1.5

Step-2: Install & Configure Turbo C

(a) Run the setup program. Just click the file (freecommandLinetools.exe)

By default, it installs to C:\Borland\BCC55 (This tutorial assumes this location).

(b) Add the /bin directory to the system PATH

In WinXP machine – you will add it under the “user variables” tab with name as “PATH”


(c)Create a configuration file for the bcc32 program that tells it where to find its libraries and include files.

Open a terminal or command prompt and type the following commands:

echo -I"C:\Borland\BCC55\Include" >> C:\Borland\BCC55\Bin\bcc32.cfg

echo -L"C:\Borland\BCC55\Lib" >> C:\Borland\BCC55\Bin\bcc32.cfg

echo -L"C:\Borland\BCC55\Lib" >> C:\Borland\BCC55\Bin\ilink32.cfg (Optional)

Step-3: Create Java class

Create a class (HelloWorld.java) that declares the native method


class HelloWorld {

static {
System.loadLibrary("HelloWorld");
}

private native void print();

public static void main(String[] args) {
new HelloWorld().print();
}
}


Compile the HelloWorld source file


javac HelloWorld.java



Step-4: Generate C Header file


Use javah.exe (included in JDK bin) to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation.

javah -jni HelloWorld


This will generates a file named HelloWorld.h.


Step-5: Writing the C implementation


The JNI header file generated by javah helps you to write C/C++ implementations for the native method. The function that you write must follow the prototype specified in the generated header file. The implementation (HelloWorld.c) looks as follows:





#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"


JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World! using JNI \n");
return;
}


Step-6: Include JNI specific header


The C program just created will need the jni.h and other header files located in the

C:\jdk1.5.0_13\include and its subfolders.


For some reason I was not able to configure the bcc32.cfg, created earlier to read the location of these JNI specific header files from their original location.


So I have copied all the header files to C:\Borland\BCC55\Include


Step-7: Compile the C implementation into a native library


Its also perfectly ok to generate a .so file but or this example I use dll


Open a cmd and hit


bcc32 -WD HelloWorld.c




The above command will compile the HelloWorld.c generating some intermediary files along with HelloWorld.dll which is what we are interested in


The –WD option tells the compiler to generate a Windows specific DLL.


Step-8: Running the HelloWorld program

Running the HelloWorld program

Because the HelloWorld class contains a main method, you can now run the program on WinXP as follows:


java HelloWorld

You should see the following output.


output:

 
Hello World! Using JNI
 
You are done!

Conclusion:

The above tutorial shows a very
basic example of JNI using Turbo
C and JDK. I have not written
complex native programs using
this compiler (like performing
arithmetics or other System
programming), So I am not sure
of the scope of JNI you can write
using this compiler.

Turbo C++ is old and JNI include files might not support all the
language specific features.
 
Do let me know if this tutorial
was useful.
 
Write remarks/feedback or any issues in the comments section below.

3 comments:

loganathan said...

Hi

When trying this in 64 bit windows the dll getting created successfully but, i am getting java.lang.UnsatisfiedLinkError. Is we need to use different Borland c++ Compiler for 64 bit?

Vishal said...

Hi
#include is not working for me. I have copy jdk1.5\include sub folders to the Borland\bcc5\include
How to do this. and how to generate
dll file.

cheapest payday loans said...

I have a .h file (downloaded) and I have the corresponding java classes.

Can I create the dll file of this .h(header) file without having the .cpp file?

 

Share/Bookmark