You are on page 1of 21

USING JAVA NATIVE INTERFACE

FOR CONNECTING FRONTEND


AND BACKEND COMPONENTS
OF OUR PROJECT…
By:
Mohit Gupta
Project Intern
TCS Innovation Labs
Current Scenario
1. We have designed programs to calculate option prices and
various greeks along with them.
2. We need to make a user interface so that a normal user can
use the product without handling the functions and programs.
3. C++ doesn’t have a good GUI making capability.
4. The team decided to develop a GUI using java swing…

So we need to assure the reusability of all the work


already done in C++…
JAVA NATIVE INTERFACE - I
Interface – Refers to a point of connection
between two components…
Backend – C++ (Option price and greeks
calculating programs)
Frontend - GUI being developed in JAVA using
JSwing library
JAVA NATIVE INTERFACE - II
It’s a framework that helps us to
connect JAVA to other languages
using a special class of functions
called native functions ...
1. Create a class
that declares
Native methods

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

JNIEXPORT void JNICALL


Java_Example_fnctname(JNIEnv *, jobject);

#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>

JNIEXPORT void JNICALL


Java_Example_fnctname(JNIEnv * jenv, jobject obj)
{
class sample
{
public void function(...arguments...)
}
}
Cppfile.cpp
#include <jni.h>
#include "HelloWorld.h"
#include <iostream.h>

JNIEXPORT void JNICALL


Java_Example_fnctname(JNIEnv * jenv, jobject obj)
{
class sample
{
public void function(...arguments...)
}
}
Cppfile.cpp
#include <jni.h>
#include "HelloWorld.h"
#include <iostream.h>

JNIEXPORT void JNICALL


Java_Example_fnctname (JNIEnv * jenv, jobject obj)
{
class sample
{
public void function(...arguments...)
}
}
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 5. Compile C++ to


2. Use “javac” to generate native
compile the library
program

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

java.lang.UnsatisfiedLinkError: no example in shared library path


at java.lang.Runtime.loadLibrary(Runtime.java) at
java.lang.System.loadLibrary(System.java) at
java.lang.Thread.init(Thread.java)

It is basically a result when we are not able to use correct path of


the .so library.

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.

The main technicality is connecting various button actions to


the native functions.

It will be easy and better to just have a look at the code and
get a feeling how this is done.
Thank You

You might also like