participate


Java Native Interface (JNI) - Calling DLL from Java
<<   Back to Forum  |   Give us Feedback
3 Duke Stars available
This topic has 27 replies on 2 pages.    1 | 2 | Next »
Sarka
Posts:23
Registered: 6/11/98
Calling DLL from Java   
May 14, 2003 9:35 AM

 
Hello!
I have some DLL and I need to call it from Java. What is the simpliest way how to do it? Should I generate some stub for this DLL?

Thank you very much
 
andrew_malcolm
Posts:2,140
Registered: 11/12/01
Re: Calling DLL from Java   
May 16, 2003 1:07 AM (reply 1 of 27)  (In reply to original post )

 
Yes, you need a JNI wrapper DLL for your DLL. Have a search through these forums, as this topic has been covered many times.
 
nishil
Posts:20
Registered: 3/4/98
Re: Calling DLL from Java   
May 16, 2003 1:38 AM (reply 2 of 27)  (In reply to original post )

 
check out this JNI documentation
http://java.sun.com/docs/books/tutorial/native1.1/conce

This is defnitely the way to do access dll's
 
a_zelin
Posts:25
Registered: 10/24/00
Re: Calling DLL from Java   
May 16, 2003 3:19 AM (reply 3 of 27)  (In reply to #2 )

 
Define a class you want to access the DLL, declare native methods in it, generate C++ headers from the class with javah, call your DLL in methods you generated headers for, build second DLL, call it from java.
 
Sarka
Posts:23
Registered: 6/11/98
Re: Calling DLL from Java   
May 19, 2003 6:22 AM (reply 4 of 27)  (In reply to #3 )

 
What should I use to write and compile .cpp files? I have tried to use Rhide with DJGPP, but it seems to me that it cannot compile things around JNI. Do I have to use Microsoft Visual C++?

Thanks
 
swatdba
Posts:1,072
Registered: 6/2/97
Re: Calling DLL from Java   
May 19, 2003 6:39 AM (reply 5 of 27)  (In reply to #4 )

 
You shouldn't have to use Visual C++. I haven't used Rhide but I have had no problems with gcc/g++.
 
Sarka
Posts:23
Registered: 6/11/98
Re: Calling DLL from Java   
May 19, 2003 7:22 AM (reply 6 of 27)  (In reply to #5 )

 
Our gcc does not know __stdcall etc. Does anybody of you know, what to do with it?
 
andrew_malcolm
Posts:2,140
Registered: 11/12/01
Re: Calling DLL from Java   
May 19, 2003 7:31 AM (reply 7 of 27)  (In reply to #6 )

 
Those keywords specify calling conventions (parameters read leaf to right, caller clears stack etc.) You'll have to read the doc for both compilers to find the equivalents. I'm a bit suprised that there aren't macros in the gcc distribution to deal with this. I'd look at the gcc doc/web site for any advice on porting VC code to gcc.
 
michbau
Posts:13
Registered: 1/8/98
Re: Calling DLL from Java   
Mar 22, 2004 10:34 AM (reply 8 of 27)  (In reply to #7 )

 
I am not ready with my studies of gcc an cygwin but I will give you, what I have already now.
May be it works for you.
$ gcc -Wall -mno-cygwin -Wl,--add-stdcall-alias -D_JNI_IMPLEMENTATION_ -I/cygdr
ive/c/j2sdk1.4.2_04/include -I/cygdrive/c/j2sdk1.4.2_04/include/win32 -shared yourfile.cpp -o yourresult.dll

-Wall
switch on all warnings
-mno-cygwin
Switch off the use of the standard cygwin libraries (posix-standard)
-Wl,--add-stdcall-alias
Advises the linker to add standard calls to the export as alias.
-D_JNI_IMPLEMENTATION_
Defines the JNI_IMPLEMENTATION -switch (used inner jni.h as ifdef switch).
#ifdef JNI_IMPLEMENTATION
#define JNI_IMPORT_OR_EXPORT JNIEXPORT
#else
#define JNI_IMPORT_OR_EXPORT JNIIMPORT
#endif
-shared
Advises the compiler to produce a shared library, this obsoletes the -dll switch.

Look at
http://forum.java.sun.com/thread.jsp?forum=4&thread=5500

There are further informations for a 2 Step work.

I hope it helps

mike
 
michbau
Posts:13
Registered: 1/8/98
Re: Calling DLL from Java   
Mar 22, 2004 10:44 AM (reply 9 of 27)  (In reply to #8 )

 
Look at:
http://forum.java.sun.com/thread.jsp?forum=31&thread=479321&message=2230324#2230324
may be, you miss parts of your devel installation.
 
michbau
Posts:13
Registered: 1/8/98
Re: Calling DLL from Java   
Mar 29, 2004 6:57 AM (reply 10 of 27)  (In reply to #9 )

 
Because I want to earn my first Duke Dollars I will give you a further hint:
http://forum.java.sun.com/thread.jsp?forum=52&thread=471668
I hope you get all you need!

mike
 
tajathegreat
Posts:1
Registered: 12/12/06
Re: Calling DLL from Java   
Dec 12, 2006 3:21 AM (reply 11 of 27)  (In reply to original post )

 
Using VC++ dll call a native method in java using JNI
(Complete solution)


1.))First you need to create A simple java file like this

public class HelloWorld {
public native void Hello();

static {
System.load("C:/calldll/DoWell.dll");
System.out.println("Loaded");
}

public static void main(String[] args) {
new HelloWorld().Hello();

}}

use load() insted of loadlibrary() and give the absolute path of dll in the load function


2.))
now compile this you will get a HelloWorld.class file
it will show loaded and a error message UnsatasifiedLinkError
on the Hello() Native method
there is no need to worry about continue ahead you will find why it is showing this when you be able to run this


3.))
now Use javah command on a command prompt to generate the .h file
javah HelloWorld it will give you a .h file .h file will look like this


/ DO NOT EDIT THIS FILE - it is machine generated /
#include <jni.h>
/ Header for class HelloWorld /

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/
Class: HelloWorld
Method: Hello
Signature: ()V
/
JNIEXPORT void JNICALL Java_HelloWorld_Hello
(JNIEnv
, jobject);

#ifdef __cplusplus
}
#endif
#endif



4.))
Now you have a .h file use this to create a HelloWorld.c file which is in form of c
it will look like this

#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
#include <string.h>

JNIEXPORT void JNICALL Java_HelloWorld_Hello(JNIEnv env , jobject obj)
{
printf("Hello world!\n");
return;
}

pay attention to JNIEXPORT void JNICALL Java_HelloWorld_Hello(JNIEnv
, jobject) call you have to modify this when you write .c file



5.))
Now the important step come in you need to download mingw software so that you can run gcc command

once you have this in place

you go to command prompt and follow these steps(depends upon where you install mingw)
C:\>Cd mingw\bin
C:\mingw\bin>

now you need to use following command to generate .o file(i show you how)

gcc -c -I"C:\program files\Java\jdk1.5.0\include" -I"C:\Program Files\Java\jdk1.5.0\include\win32" -o "C:\calldll\HelloWorld.o" "C:\calldll\HelloWorld.c"

you have to run this command on

C:\mingw\bin\>

this will create HelloWorld.o file

-I"C:\program files\Java\jdk1.5.0\include" in this you need to mention your PATH of jdk in my case this is (C:\program files\Java\jdk1.5.0\include)

and in

-o "C:\calldll\HelloWorld.o"

you need to specify loc where you want to have this HelloWorld.o file(You should include all the files in one directory in my case it is calldll)

"C:\calldll\HelloWorld.c"

and this is the path of HelloWorld.c file

After this you now have a .o file

6.))
now you have to write HelloWorld.def file like this

EXPORTS
Java_HelloWorld_Hello

where HelloWorld is the name of the class and _Hello is the native method name
save it in the same directory calldll in my case

now you have to create a new dll HelloWorld.dll that will provide communication between java and other language

Use this command to generate a new dll

gcc -shared -o"C:\calldll\HelloWorld.dll" "C:\calldll\HelloWorld.o" "C:\HelloWorld\HelloWorld.def"

it will greate the new dll HelloWorld.dll overwrite the previous one


7.))now you have done all steps

now open a new command prompt window

and compile and run the HelloWorld program
C:\calldll>javac Helloworld.java
and C:\calldll>java HelloWorld


It will give you an output

Loaded
Hello World!
 
mdenty
Posts:336
Registered: 16/06/99
Re: Calling DLL from Java   
Dec 12, 2006 12:36 PM (reply 12 of 27)  (In reply to #11 )

 
Hi,

Alternatively if you are not the C/C++ god, you can use a generic DLL wrapper like JNative which is free and under LGPL.

Try it, it can save you hours of coding (or cost you hours of headaches ;) .

--Marc (http://jnative.sf.net)
 
GirishK_12
Posts:7
Registered: 12/20/06
Re: Calling DLL from Java   
Dec 20, 2006 3:57 AM (reply 13 of 27)  (In reply to #12 )

 
Sample example given in many of the sites are very simple in the sense that you create a dll of your own and invoke a function in it. Initial question in this thread is that how to use an already existing dll say X.dll. Following are the steps
1. We need to create an intermediate JNI wrapper, which is a pure C libray say Y.dll, which internally invokes X.dll.
2. Load Y.dll file in your java class and invoke the function.

Well, currently I am working on this. I am able to get through the first step. In the second step, I am able to load Y.dll file but when I invoke a function JVM terminates because it is not able to locate X.dll referenced by Y.dll.

Can anybody suggest me how to go about?
 
mdenty
Posts:336
Registered: 16/06/99
Re: Calling DLL from Java   
Dec 20, 2006 11:37 AM (reply 14 of 27)  (In reply to #13 )

 
Hi,

Try to put Y.dll in the PATH.
Or do System.load("Y:/path/to/your/lib/Y.dll"); before loading X.dll

--Marc (http://jnative.sf.net)
 
This topic has 27 replies on 2 pages.    1 | 2 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums