Part 2: Proper Compilation and Organization of our files
What we did in the first part was to dump all the files to one folder, and then compile and run everything from there. This is how my folder looks up to here:

It’s messy when all the files are dumped into one folder
With this mess, we won’t get very far. So, Let’s start organizing things (here we will start using our IDE).
On you computer somewhere, create a folder and call it JNI Tutorial. Inside it, create another folder called JNI and create a C/C++ project (called JNI Tutorial – after the name of our DLL) in that folder, using your C/C++ IDE.
My folders look like this: (It will look slightly different – depending on your IDE. The important part is that you have a source folder, and a JNI folder):

A basic folder structure to seperate our java source and JNI source
Create a package in the java project, called tutorials.jni and create a new class in it called Program1 (copy all the methods from Program1.java above). Notice that as soon as you do this, you should see two new subfolders.
Using your java IDE, create a project in that folder with the name JNI Tutorial. Your folder will now look something like this (depending on your IDE):

Organizing our java files in packages creates additional folders
Compile Program1 (this will output a .class file) using your IDE (or with the command line using javac). Make sure that the compiled class is in a new folder called build or something like that, and it’s in subfodlders according to it’s package, otherwise javah won’t work!

Notice that the output of the compiled files are organized by their package
To create the header files, you will need to open a command line and change to the folder which is above the subfolders of your package (in my case, I’ll change to the JNI Tutorial/build/classes folder). Then, type javah FULL_CLASS_NAME.
You can add these optional arguments before the class name:
- –o FILE_NAME – This will specify the name of the output file – this can be simply a filename or an absoloute/relative path to the file, with the filename at the end. When specifying a specific folder, It must exist before the file’s creation!
If you don’t use this option, the javah tool will automatically choose a name for our header file, based on the package of the java class, hoever this name can be very long. -
-d OUTPUT_FOLDER – This will specify in which folder the output file should be created. If the folder doesn’t exist, it will be created (all necessary subfolders will also be created).
Note: You can’t use both –o and –d, you can use only one of them at a time!
For example, I typed javah –o ../../JNI/Program1.h tutorials.jni.Program1, So I got a file named “Program1.h” inside my JNI Tutorial/JNI folder.
The problem is, that it’s going to be annoying to do this for each class file when you’ll have a big project… So now I’m going to give you a little tool that I have written in order to automate the creation of header files for all our class files. Download the javahUtil which I attached to this tutorial. It will automate most of the process of creating the header files. It’s bit experimental right now, however it should work in most of the cases.
Use this tool to generate all your headers into your JNI directory. Create a text file somewhere with the list of our classes by their package (One full class name on each line, with no extra linebreaks anywhere in the file):
ClassList.txt
tutorials.jni.Program1

A screenshot of the javahUtil window
Write an implentation of the function random like before, and compile it to a DLL.
Create a folder called dist (or anyother name you want), compile your classes to a jar and put it in that folder. Put the dll in the same folder as your jar. Open a command line, go to that directory and type
java –jar JAR_NAME
(replace JAR_NAME with your jar’s name)
javahUtil is an experimental java program for automating the process of generating header files for java JNI. Rigt now it’s in beta stage, and it will be released this week. Right now, it works on Windows, and I’ll check it today for Linux. I don’t have any access to macintosh so If you are willing to test it under macintosh, please comment here.

Entries (RSS)