Using Java Native Interface For Connecting Frontend and Backend Components of Our Project
Using Java Native Interface For Connecting Frontend and Backend Components of Our Project
Example.java
Example.java
class Example
{
public native void fnctname();
static
{
System.loadLibrary(“libpath/libprojectname.so");
}
public static void main(String[] args)
{
new Example().fnctname();
}
}
Example.java
class Example
{
public native void fnctname();
static
{
System.loadLibrary(“libpath/libprojectname.so");
}
public static void main(String[] args)
{
new Example().fnctname();
}
}
Example.java
class Example
{
public native void fnctname();
static
{
System.loadLibrary(“libpath/libprojectname.so");
}
public static void main(String[] args)
{
new Example().fnctname();
}
}
1. Create a class
that declares
Native methods
3. Use “javah” to
create header file
Example.java
Linktocpp.h
2. Use “javac” to
compile the
program
Example.class
Linktocpp.h
#include “jni.h”
/* Header for class Example */
#ifndef _Included_Example
#define _Included_Example
#ifdef __cplusplus
extern “C” {
#endif
#ifdef __cplusplus
}
#endif
#endif
1. Create a class
that declares 4. Write C++ code
Native methods (Implementation of
native methods)
3. Use “javah” to
create header file
Cppfile.cpp
Example.java
Linktocpp.h
2. Use “javac” to
compile the
program
Example.class
Cppfile.cpp
#include <jni.h>
#include "HelloWorld.h"
#include <iostream.h>
libprojctname.so
Example.class
Example.java
class Example
{
public native void fnctame();
static
{
System.loadLibrary(“libpath/libprojectname.so");
}
public static void main(String[] args)
{
new Example().fnctame();
}
}
1. Create a class
that declares 4. Write C++ code
Native methods (Implementation of
native methods)
3. Use “javah” to
create header file
Cppfile.cpp
Cppfile.cpp
Example.java
Example.java
Linktocpp.h
Linktocpp.h 5. Compile C++ to
2. Use “javac” to generate native
compile the library
program 6. Run Java
program using Java libprojctname.so
interpreter libprojctname.so
Example.class
Example.class
OUTPUT
Each element of Java language must have a corresponding native
counterpart
A commonly Occurring Error
Other Features
1. We can call java functions in native functions.
2. Java arrays can be handled.
FRONTEND DEVELOPMENT USING
JSWING
I am using NetBeans IDE to develop the frontend for the
project.
It provides a simple framework where we can Drag and Drop
various components such as Tables, buttons, Text fields and
develop a GUI.
It will be easy and better to just have a look at the code and
get a feeling how this is done.
Thank You