Running C code from Java (Java Native Interface)

Running C code from Java can be challenging. In this example, I used calculator code in C++ and experimented by calling its functions in Java code.

Here are the steps to call C/C++ functions from Java.

Step 1:

We shall start with some C/C++ code whose functions need to be called from Java code. I’m using following simple calculator functions in C:

You can use the same code and save it as Calculator.C file. We shall be using sum, sub, dvd, and mul for summation, subtraction, division, and multiplication respectively. We also need to create a corresponding file in Java (Calculator.java) with native function signature as follows:

Note that CalLib will be created later i.e. a DLL file with the same name shall be created.

Step 2:

Now we have to compile created java source file “Calculator.java”. Open Command prompt in the same directory as of “CaIculator.java”. Write command  javac Calculator.java

Step 3:

Now we have to create the C header file of created java source file “Calculator.java”. Open Command prompt in the same directory as of “CaIculator.java”. Write command javah jni Calculator .This will create C header file as Calculator this file will be used to make DLL file later.

Step 4:

The next step is to combine the C/C++ and the generated header file. Open a new text file, write down three #includes as below. Open copy C header file, copy all the code just from the line “JNIEXPORT jfloat JNICALL Java_Calculator_sum” upto the end. Now just declare a variable (in this case x and y) just after the function data type (in this case float) as (JNIEnv *, jobject, jfloat x, jfloat y), save this file as Calculator.cpp. The file should now look as shown below:

Step 5:

Open Dev-C++, goto new>file>project this will open a window as:

Dev-1

Choose DLL, C/C++ Project and give the name (it should be the same as mentioned erliar, in this case CalLib) click OK. Save it with default extension .dev and remove the default generated files by right clicking on each file and removing it. Now, add Calculator.h and Calculator.cpp in the project:

Dev-5

Once you select and add both files, the next step is to add “include directories”.  Right click on project and select project options. Go to directories -> include directories and add the JDK paths as shown in the following figure:

Dev-8

Click OK. Go to Menu>Execute>Rebuild All or press F12. Once build, your DLL is ready to be used from within the Java Code.

Step 6:

Open Eclipse>File>New>Java Project give it a name (in this case CalculatorRunner) click Finish.

Right click on Project>New>Class

eclipse1

Give it a name, tick public static void main(String[] args) option and click Finish. Write the following code in the file:

Copy Calculator.java and CalLib.dll (created from step 1 and 5, respectively) to src folder folder of the current project where you shall be calling the C functions. The one final missing piece is adding the native library location. Go to Project>Properties (Alt+Enter) this will open a windows as:

eclipse4

Select Java Build Path, expand the project (CalculatorRunner), select and double click the Native library location option this will open a window as:

eclipse6

Select the workspace and the src folder:

eclipse7

You have fixed the library path issue. Now you can call and test c functions from Java code.

I hope it will help you understand the process of calling C functions from Java code. Happy Coding! 🙂

ShareTweet about this on TwitterShare on Facebook0Share on LinkedIn0Pin on Pinterest0Share on Reddit0

Leave a Reply

Your email address will not be published. Required fields are marked *