codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || RAM 
Search Site:



Home » JAVA » Article

Java Native Interface (JNI)

Article by: Kanad Deshpande (4/15/2005)
Bookmark us now! Add to Favourites
Email a friend! Tell a friend
Summary: Java Native Interface (JNI) is one of the intersting interface by java By using Java Native Interface (JNI) you can operate with other applications and libraries.
Viewed: 134036 times Rating (43 votes): 
 4.3 out of 5
  Rate this Article   Read Comments   Post Comments

Java Native Interface (JNI)



JNI is the native programming interface for java that is part of JDK. Using JNI you can operate with other applications and libraries written in other language such as C,C++. But the basic question arises when should I use the JNI ?
  1. You want some platform specific information and the standard Java class library may not support the platform-dependent features needed by your application.
  2. You have some library application written in other language and you want to use it in your java application.
  3. You want Java should interact with some low level programming language.

Below is given Simple Example

See that methods have 'native' KeyWord

public native void displayHelloWorld();
public native void displayOther();
private native String getLine(String prompt);

The DLL we are going to use is firstJNI.DLL This DLL can be generated by VC++ or borland. Which we will discuss later.

Select All Code


Compile the above code using
prompt>javac firstJNI.java


Then create header File using
prompt>javah javah -jni HelloWorld

This will create firstJNI.h file
In the header File you will see
-------------------------------------
JNIEXPORT void JNICALL Java_firstJNI_displayHelloWorld
(JNIEnv *, jobject);

/*
* Class: firstJNI
* Method: displayOther
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_firstJNI_displayOther
(JNIEnv *, jobject);

/*
* Class: firstJNI
* Method: getLine
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_firstJNI_getLine
(JNIEnv *, jobject, jstring);
----------------------------------------------
Don't edit header File

Now the real part comes - making the DLL!. First we see generating the DLL using the Borland compiler. Now we will write some simple C Code



Select All Code


In the above code, the first two methods only Display message, while the third one takes input from the user
The Syntax For Declaring Methods
--------------------
JNIEXPORT <return type> JNICALL
Java_<className>_<methodName>(JNIEnv *env, jobject obj)
{
...........
}
-------------------

To generate DLL
create bcc32.cfg file in %BORLAND_INSTALL%/BCC5/bin Folder
In the bcc32.cfg file add following code
-I"%BORLAND_INSTALL%\BCC55\include;%JAVA_HOME%\include;%JAVA_HOME%\include\win32"
-L"%BORLAND_INSTALL%\BCC55\lib;%BORLAND_INSTALL%\BCC55\Lib\PSDK;"
i.e the path for borland compiler and java.

now goto prompt and say
prompt>bcc32 -tWM -tWD firstJNI.c

This will create firstJNI.lib File
Now say
prompt>implib -c firstJNI.lib firstJNI.dll

That's it!!
It will generate DLL file.
Now you can run the firstJNI.class file by using java.
--------------------
Now let see how to generate DLL using VC++
Click File->New->Win32Dynamic-Link Library
Give name and Select
A simple DLL project
You will have
firstJNI.CPP file
Below is given the firstJNI.cpp file


Select All Code


The syntax declaration is same as discussed above. In VC++ it may throw error for unable to include for jni.h or firstJNI.h where you have to give path for these header files while including.

The above code will also check for RAM size and print it. Borland compiler do not provide bios.h so it is not included in the code. Build all Application and you will get the DLL file. Go to prompt and run firstJNI.class using java. So here we can print RAM size!!! Now since this is the basic we can go further with this. If you don't set the library path correctly it will throw exception
java.lang.UnsatisfiedLinkError: no hello in shared library path
at java.lang.Runtime.loadLibrary(Runtime.java)
at java.lang.System.loadLibrary(System.java)
at
at java.lang.Thread.init(Thread.java)

so copy library(dll) in same folder.




Useful Links


CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Java Native Interface (JNI)'
RELATED ARTICLES
Java MP3 Player
by David Barron
A fully functioning MP3 Player with complete source code available for download
A simple way to JTable
by Kanad Deshpande
Many face trouble while dealing with JTable. Here is simplest way to handle JTable.
ID Verification using JSP
by Nouman Rashid
One of the most important parts of web development is to make sure that only authorized users get access to certain areas of the site. This tutorial takes a look at various steps involved in making JSP pages which validate a user ID and password from a MS Access database which contains the username and password.
Java Native Interface (JNI)
by Kanad Deshpande
Java Native Interface (JNI) is one of the intersting interface by java By using Java Native Interface (JNI) you can operate with other applications and libraries.
Understanding Hibernate ORM for Java/J2EE
by Saritha.S.V
Hibernate is the most popular and most complete open source object/relational mapping solution for Java environments.Hibernate's goal is to relieve the developer from 95 percent of common data persistence related programming tasks.
Login codes with JSP,JavaBean from mySQL database
by Prakash
my problem is can i have the code to login with the username and password using JSP and JavaBean/Servlets from mySQL database.When the user enters the username and password in the login page then it will go to the requested site.How to do it?
Java Speech Synthesizer
by David Barron
Small and simple. Type a sentence and press enter and your computer will speek to you.
simple Java Development Environment
by David Barron
Program in JAVA with ease, using this development environment, or adapt it to your own needs.
Turn EJB components into Web services
by Krunal J Patel
Web services have become the de facto standard for communication among applications. J2EE 1.4 allows stateless Enterprise JavaBeans (EJB) components to be exposed as Web services via a JAX-RPC (Java API for XML Remote Procedure Call) endpoint, allowing EJB applications to be exposed as Web services. This article presents a brief introduction to JAX-RPC, outlines the steps for exposing a stateless session bean as a Web service, and provides the best practices for exposing EJB components as Web services
CORBA Technology
by Krunal Patel
CORBA defines an architecture for distributed objects. The basic CORBA paradigm is that of a request for services of a distributed object. Everything else defined by the OMG is in terms of this basic paradigm.








Recent Forum Threads
•  Run a program both on windows and linux
•  VERO.SurfCAM.v2014
•  Schlumberger.Petrel.V2013.2
•  Petrel.V2013.2
•  Altair.HyperWorks.v12
•  VoluMill.v6.1
•  VoluMill.NEXION.6
•  VERO.SurfCAM.v2014
•  Schlumberger.Petrel.V2013.2


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2015