The Java Native method is a great way to gain and merge the power of C or C++ programming into Java.
Writing native methods involves importing C code into Java application. We'll follow few steps to create native methods -
1. Writing and Compiling Java Code
2. Creating C header (.h file)
3. Writing C code
4. Creating shared code library (.dll file) for Windows and (.so file) for Unix
5. Run Application.
In our example, we are using gcc compiler for C and considering path for gcc and java are set in the environment. Here we will create two methods - printMessage() which will print message in the console and printSum() which will print the sum of two numbers.
Project Structure -
Step 1 : Writing and Compiling Java Code
(NativeClass.java )
package com.anjan.api;
public class NativeClass {
static{
System.loadLibrary("nativedemo"); //Load native library at runtime nativedemo.dll (windows) and nativedemo.so (unix)
}
public native void printMessage();
public native int printSum(int a, int b);
}
(MainClass.java)
package com.anjan.main;
import com.anjan.api.NativeClass;
public class MainClass {
public static void main(String args[]){
NativeClass cls = new NativeClass();
cls.printMessage();
System.out.println(cls.printSum(5, 3));
}
}
After writing the Java Code, compile the code. After compiling the code, .class files will be generated in bin folder of the project.
Step 2 : Creating C header file (.h file)
To generate header file, follow the below steps -
(a) Go to the command prompt.
(b) Go to the bin directory of the project
e.g - cd C:\NativeDemo\bin
(c) We'll use javah command to generate header file. Execute the below command to generate header file (.h file) for NativeClass
e.g - javah com.anjan.api.NativeClass
A file com_anjan_api_NativeClass.h will be generated in bin folder. Create cfiles directory in the project and copy the header file into it.
(com_anjan_api_NativeClass.h)
Step 3 : Writing C Code
Create NativeClass.c inside cfiles folder and we'll write the logic for printMessage() and printSum() method in C file.
(NativeClass.c)
Step 4 : Creating Shared code library (.dll file)
To generate the shared code library (.dll file), we will go to cfiles directory in command prompt and we execute the below command.
gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o <dll file name> <cfilename>
e.g -
gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o nativedemo.dll NativeClass.c
After executing the above command .dll file will be generated inside cfiles directory.
The dll file name should be same as the library name provided to load library in Java class.
Step 5 : Run Application
To execute the application, we will have to set the VM arguments for the application.
To set the VM arguments, follow the below steps -
(a) Right click on the Project and go to Run As -> Run Configurations
(b) In main tab, browse the project and enter the Main class of the project. Click Apply
(c) Go to Arguments tab, and enter the VM arguments and Click Apply and Run
-Djava.library.path=<directory containing dll file>
e.g -
-Djava.library.path=cfiles
Output -
I am Native Method
8