JNI Tutorial

Java Native Interface (or simply JNI) is an interface of the java virtual machine, which allows you to extends your java programs to use functions which are not written in java. This means that we create a Dynamically Linked Library (DLL) in C/C++ and then we call function from it from inside out java code. In this tutorial, you will learn how to extend your java programs using JNI.

Example usage of JNI: SVG is a graphics file format which is based on vector instructions (this means that instead of remembering information about the pixels, we remember information about how to draw the image using shapes, fills and lines). In order to view these files and convert them to pixel based images, there is a library in C called librsvg.
Instead of rewriting this library in java, we can simply bind it’s functions to java using JNI. We will bind the functions to java, and we will convert the image buffer of librsvg to an image buffer that java can view.

Perquisites:

  • Good knowledge of java and full understandment of java objects.
  • Basic knowledge of C/C++ is mandatory. You should also have a general idea of what is a DLL.
  • A C/C++ compiler.
  • Sun’s  Java Development Kit (JDK).
  • *An IDE (Interactive Development Environment) for Java and for C/C++. I’ll be using Netbeans for Java and Microsoft Visual C (MSVC) for C/C++. Other common IDE’s that you can use are Eclipse and Anjuta.
  • Basic knowledge on how to use the command line (aka Terminal). If you don’t know, you can read a bit more about it in the last page of this tutorial.

*You can also use any simple text editor for editing the code, and use the command line for compiling stuff, yet this is tedious and unnecessary. I will not cover how to use different IDE’s, so choose one that you know how to use.

Advatages of JNI

  •  No need to rewrite code that already exists – you only need to bind the existing code and this is usually much simpler.
  • Functions are loaded from a DLL, so you don’t need to update you java code in order to update the function. Simply replace the DLL with a new one and the java virtual machine won’t even notice it.
  • Allows you both to use the advtages of an object oriented language such as java, and still use low level api that is only available in languages such as c.

Disadvatages of JNI

  • Pure java programs are almost completly portable, and JNI isn’t. You must recompile the DLL’s on each platform, and you must keep your C\C++ code cross platform.
  • Requires installation of additional components (the required DLLs) on each computer that uses it.

Table of contents:

  • Part 1 - In the first part of this tutorial, we will look on a basic JNI example and we’ll understand the basics of using native methods. We will also see how to compile JNI programs.
  • Part 2 - In this part, you will learn how to properly organize our work environment to work from both C and C++. We will also fix several compilation issues that might rise in the future.
  • Part 3 - This parts teaches to acces java fields, methods and objects from inside our C code, using the JNI environment.
  • Part 4 - Teaches how to use and convert java arrays to c arrays and vice versa
  • Part 5 - Teaches how to handle C pointers from java.
  • Appendix 1 - How to use the command line (aka terminal, console). This covers only a few basics. I reccomend finding a propper tutorial for this, based on your operating system.
Leave a Reply