In static block, we are loading a DLL named TestDll.dll which should be placed in <WINDIR>/SYSTEM32 directory. I am not saying you cannot put this DLL in other directories, you can, but then you will have to register that path in JVM. Here you can call another method as well, instead of loadLibrary().

System.load(“c://winnt//system32//TestDll.dll”);

I think load() method is better as far as usage is concerned because you can easily place your DLL in any directory.

publicnativevoid inDll();

As you can guess, this native keyword specifies that inDll() is a native function which is implemented in the library. Also noticeable, there is no implementation. When we call this function in our code, JVM will look for the function in the loaded DLL (due to static block, DLL will be loaded immediately after the loading of the class) and then call that function. Otherwise it will throw java.lang.UnsatisfiedLinkError exception.

Compile this class normally. Now we will need to generate header file of this class.

You can use javah.exe utility which is included with JDK.

Javah -jni Test1

Header file is generated in current directory with the name Test1.h. Keep this header file safe as we will use this soon.

Now we are going to write a DLL which will contain the implementation of our native function. I have chosen MFC AppWizard (DLL) in project and named my project: TestDll.

Include your class header file which was generated previously using javah.exe in TestDll.h.

Copy following line from Test1.h and paste it at the end of TestDll.h.

Now compile and generate your DLL, put newly created DLL in system32 directory and then run your class file. It should work properly. If you receive an error like UnsatisfiedLinkError, then recheck your procedure. If you are using any IDE, then try using command prompt. Hope it will work.

In this tutorial, I have tried to explain the basics of JNI. I will write a second tutorial of the series soon in which I will explain how to pass parameters to native functions and return something, along with a practical example.

Waiting for your comments and questions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

I wasn't able to get javah to reconize my Java class. Was able to figure it out though. Had to add full package name of the Java class when calling javah. See blog for more info: http://floydsdiner.blogspot.com/2011/12/lunch-special-dont-forget-package-name.html

Hi
why cant i directly give the folder name where the .h file is created,
i gave it and i got new error:
Error 1 error C2665: 'AfxMessageBox' : none of the 2 overloads could convert all the argument types ...\myworkspace\msapp\testdll\testdll\testdll.h 33 TestDll

I like the post. However there are some amendments that can I would like to suggest:
1. Add the JDK directory to Tools=>Options=>Directories.
2. Include the line #include "Test1.h" to "TestDll.h" file before compiling.

Hi,
I have an ancient software written in GFA BASIC. I've to change its GUI.
Could anyone please tell me if its possible to use Java SWING and bridge the gap using JNI (without having to change the original code)??
Thanks in advance.

MY4JAVA_*projectname*_API bool func(char *);
--------------------------------------------------------
It produces a DLL when building in VC2005 with the name *projectname*.dll with the func as a function inside the DLL.

Hi,
I am having a .LIB file. I am not sure if it is same as .DLL file.
Can I read that LIB file using the code given?
Also I dont have VC++ to create a dll file. Can I create it without VC++?
Or could you send me the test dll file?
¬Sharad.

I don't think it can... .lib files are used at link time to create "C end products" (.exe, .dll), so you'll probably want to link you .dll against the .lib file you desire and then call into that dll (using jni or jna).

Hi,
I have to call the methods of a DLL that already exist. It does not have JNIEXPORT .... etc.
Is it possible to use that DLL without new Implementation? How to do that?
I have to use 6-8 DLLs, do I need to all the DLLs again to wrap them for JNI?

Hi,
Great article ! but JNI binds you to the platform , which limits the W.O.R.A principle. To access COM (only COM) components (inproc or exe servers) from any platform supporting Java, you can also try j-Interop (http://j-interop.sourceforge.net) which implements DCOM protocol completely and gives true platform independance.

Some hints might be helpful for others, as I fear this post will not be fixed any more. (took me half an hour to get it running, so I have another one for this post):
- VS2005 MFC DLL creates a lot of stuff to ignore, just be sure the framework compiles before you change or add anything

- Just add the Java_Test1_inDLL() method to the end of your DLLTest1.cpp

- usually CLASSPATH is an issue for javah and java.
For me, by default the current directory is not in the CLASSPATH environment variable, but it helps a lot to do so.

- The recommended alternative library load should read:

System.load("c:/<full_path_in_single_forward_slashes>/DLLTest1.dll");

- It should run or debug (the java part) inside Eclipse
(I had no problem with Eclipse Europa and jre 1.6).
But: Does someone know how to javah from within Eclipse?

hi,
i have a final project. the first step is to move all source code in java.net to another name package. for example, tcd.net package. but this give me errors. i can't run my socket application when i using import tcd.net.*; statement. it give me this error UnsatisfiedLinkError.

Does anyone know how to do the simulation to aswer 3
questions at the bottom? if so help me. I have the program
running but I don't know how to find the answers to the
questions. The problem is below

Write a program that simulates a checkout line at a
supermarket. The line is a queue object. Customers (i.e.,
customer objects) arrive in random integer intervals of
from 1 to 4 minutes. Also, each customer is serviced in
random integer intervals of from 1 to 4 minutes. Obviously,
the rates need to be balanced. If the average arrival rate
is larger than the average service rate, the queue will
grow infinitely. Even with “balanced” rates, randomness can
still cause long lines. Run the supermarket simulation for
a 12-hour day (720 minutes), using the following algorithm:

a) Choose a random integer between 1 and 4 to determine
the minute at which the first customer arrives.
b) At the first customer’s arrival time, do the following:
Determine customer’s service time (random integer from 1 to
4).
Begin servicing the customer.
Schedule arrival time of next customer (random integer 1 to
4 added to the current time).
c) For each minute of the day, consider the following:
If the next customer arrives,
Say so,
Enqueue the customer;
Schedule the arrival time of the next customer;
If service was completed for the last customer;
Say so
Dequeue next customer to be serviced
Determine customer’s service completion time
(random integer from 1 to 4 added to the
current time).

Now run your simulation for 720 minutes and answer each of
the following:

a) What is the maximum number of customers in the queue at
any time?
b) What is the longest wait any one customer experiences?
c) What happens if the arrival interval is changed from 1
to 4 minutes to 1 to 3 minutes?

I’m trying to write a program using two application (1
client and 1 server) in java. What I would like for the
client application to do is when the user click a button
the client would go out to the server and retrieve
information (the information is hard coded in an array) and
display it back on the client application text area.

I am having trouble with the coding the process part.
Can anyone assist me with this?
I have added the code below. It is add in the action
listener for the button.

I am very good at HTML and CSS but i am now learning java.
I am not sure i have netbeans 5.0 RC1 adn jdk-1 5.0 installed correctly on my computer.
When i run hello world ap i don;t get any thing typed out.
Please help me if you can!Thanks

Hi,
Its really a good article.Thanks a lot Irfan Dawood.
Previously I also was not able to do.Error was in this line.:
System.load(“c://winnt//system32//TestDll.dll”);

because I am using Win98 so I have change it to.
System.load(“c:\\winnt\\system32\\TestDll.dll”);
and changeed (“ ”)to (" ") this.
I hope it will help in solving Unsatisfiedlinkerror!

But I want to use DLL which has already created in vc++. I can use it in other languages. But I want it with the help of Java.This Dll will take some parameter.And will give final result.
Please help me out.If anybody knows it.

The code worked without a hitch. Mind you I have no idea what will happen if you try and pass classes as parameters. Experiance says the class interfaces have to be passed on as well so they can be operated. But then there is the question of how the implementations are then obtained.

I am stuck up with java.lang.unsatisfiedlinkerror when I try to run a java code which connects to the C code through JNI.I have set the path in java.library.path.It finds the dll but could not get the method written in it. I have also tried renaming the method,as

Method name in DLL:java_JNITEST_test
Method called in Java:test

so I renamed the method called in java from test to java_JNITEST_test, but it still gives me the same error.
Kindly help me in solving this.