If you need to work with JNI it typically means that you have to work with native code. Sometimes you simply have no choice. Either vendor of the code is not interested in porting it to Java or, you have to work with legacy code.

JNI might be painful, especially if you are not comfortable with C language. In addition to that, at the very beginning, simple tasks look very complicated. After some time you will become familiar with syntax and everything will be much simpler.

This book is a typical cookbook. It means that I don’t provide extensive explanation of the sample code. I don’t give detailed analysis of what is happening behind your back. Instead, I give simple recipes that show how to achieve the goal. However, at the very end of book I give a list of books/locations that you, for sure, will be interested in.

The content of the book should be rather comprehensive for any average Java developer. I try to provide as simple examples as possible but no simpler. I assume that you know what Java is, and how to install JDK. I hope you know, at least, basics of C. I understand that you know how to compile C code and how to use make (but not more than just enough of it). Finally, I assume that you know how to use Command Line Interface. Samples are prepared in and for CLI.


All samples in this book use following software:

  • JDK 1.8.0_102
  • lldb-370.0.42
  • Apple LLVM version 8.1.0 (clang-802.0.42)
  • iTerm 2 Build 3.1.beta.5
  • VIM – Vi IMproved 7.4
  • macOS Sierra 10.12.4

If you don’t have command line based compiler in your environment, you have to install it. The easiest way to do it is to download Xcode from the App Store.


In this book you will find topics belonging into different categories:

  • type mappings;
  • working with simple types;
  • working with objects;
  • working with arrays;
  • dynamic reloading library;
  • working with memory allocations in native code;
  • working with Fortran codes.

I don’t divide book into chapters, sections or, whatever different means of content division you can imagine. The point here is that this book is a living one. I am sure it will grow over time and I don’t want to rearrange it each time new recipe pops in. I know it may introduce a little bit of mess, but I will try to keep it organized via labels, tags, hashtags, indexes, appendixes, etc. I hope it will work as expected.


If you want to work efficiently with the samples you have to install following software on your OS X machine. If you happen to use Linux/Windows you are (to some extent) on your own. Most samples should work out of the box in Linux. As for Windows, it might be you will require some additional setup.

iTerm 2 installation

Visit page: download iTerm 2 and install it. I suggest using iTerm 2 instead of Terminal application. This is not necessary. You can stick to Terminal as well.

JDK installation

Visit the page: and download most recent JDK release (choose proper platform). Install JDK and verify you can run it. You should be able to execute following code in terminal window

If the version of Java is slightly different it shouldn’t matter that much.

Xcode installation

Visit App Store, find and install Xcode application. During installation process it should install command line applications as well.

LLDB installation

Check, whether you have lldb installed and whether you can use it.

If you can’t execute lldb it means that it is not installed. You have to install Xcode and make sure to install command line tools (this step should be done automatically during Xcode installation).