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
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
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
Compile the HelloWorld source file
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:
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.
Running the HelloWorld program
Because the HelloWorld class contains a main method, you can now run the program on WinXP as follows:
You should see the following output.
Hello World! Using JNI
You are done!
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
Write remarks/feedback or any issues in the comments section below.