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! || Forums || 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
Sponsored by: Neil Matthews Hypnotherapy, Wilmslow
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: 51327 times Rating (34 votes): 
 4.2 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.





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)'
Posted by :  sagiragu at 01:19 on Friday, March 10, 2006
Thanks Dude
Posted by :  srodrigues at 14:35 on Tuesday, May 09, 2006
Important:

Exception in thread "main" java.lang.UnsatisfiedLinkError:
at com.wilshire.tucs.jni.example.HelloWorld.print
at com.wilshire.tucs.jni.example.HelloWorld.main

I searched till I dropped to figure this out and finally found it in a forum to get this resolved.

If you have the Java class declared in a package com.mycompany.jni.MyClass, please add it as follows.

javah -jni com.mycompany.jni.MyClass

You will get the following header file..

com_mycompany_jni_MyClass.h

with the following header signature inside this file

JNIEXPORT void JNICALL Java_com_mycompany_jni_MyClass_print
(JNIEnv *, jobject, jstring);

Include this header as is in the .c/.cpp file and after that create the DLL / .so file.

It took me 3 to 4 hours to figure this out, hope it takes you a few minutes :-)
Posted by :  kanad at 08:42 on Tuesday, December 19, 2006
srodrigues, Thanks
Sorry for my late reply though.
Plus in above

I have written
----------------------------------
Then create header File using
prompt>javah javah -jni HelloWorld
----------------------------------
It should be
prompt>javah -jni firstJNI.



Posted by :  ptobra at 01:04 on Monday, June 11, 2007
This topic is little outta JNI. I am new entry to code toad. Donno how to create a thread.

I want to create .dll for all platforms using one file and cross platform compilers. The platforms I want to include are windows, unix versions (linux, hpux, aix , solaris).
Any suggestions
Posted by :  kanad at 08:55 on Wednesday, June 13, 2007
This book explains about threads.
http://java.sun.com/docs/books/jni/html/invoke.html#9726
Please go through the book
http://java.sun.com/docs/books/jni/
About compiler tips
http://weblogs.java.net/blog/kellyohair/archive/2006/01/compilation_of_1.html
Posted by :  vikramrc at 03:07 on Sunday, July 01, 2007
Good article ! but I would like to point out that it is immensely hard to do this when you are talking to a technology like COM, keeping all their threading models , GC and interface pointer semantics.

Their exists a library in open source domain, which provides non native (JNI less) access to COM from Java. The best part is that by this virtue(lack of JNI) it can run from any platform supported by Java. More information can be found at http://j-interop.sourceforge.net

just my 2 cents,
Thanks,
best regards,
Vikram

Posted by :  kanad at 04:03 on Thursday, July 26, 2007
vikramrc,
Thanks for your link. Looks a lot interesting.
The JACOB Project: A Java-COM Bridge
http://danadler.com/jacob/ is also there. It uses JNI to make "native" calls into the COM and Win32 libraries.


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
A simple way to JTable
by Kanad Deshpande
Many face trouble while dealing with JTable. Here is simplest way to handle JTable.
Java MP3 Player
by David Barron
A fully functioning MP3 Player with complete source code available for download
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
• Re: Insert Contents of .txt file into a .html page
• Re: Perl regular expression problem.
• Re: Problems in login using WWW::Mechanize
• searching for gd::graph guide
• Re: ARRAY OF HASHMAPS
• Adv. Regexp or Otherwise
• Adv. Regexp or Otherwise
• I need help pleasee! My project is about using I/o Stream
• Help Running VB script in Windows 2003


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