Board index » jbuilder » Calling a dll func from Java

Calling a dll func from Java


2005-02-25 05:20:17 PM
jbuilder4
Hi,
I have a c dll (MyDll) stdcall fnc as follows.
stdcall int MyFnc(int FileNo, int IOType, unsigned char * DataToWrite, unsigned char * DataRead);
I try to call from java code, but it gives me an error as follows.
Exception in thread "main" java.lang.UnsatisfiedLinkError: Myfunc
at X.MyFnc(Native Method)
at X.main(X.java:6)
My java code is:
class X {
private native int MyFunc(int fileNo, int IOType, byte[] DataToWrite, byte[] DataRead);
public static void main(String args[]) {
X p = new X();
byte[] c= new byte[512];
byte[] c2= new byte[512];
int result = p.MyFunc(1,0,c,c2);
System.out.println(result);
}
static {
System.loadLibrary("MyDll");
}
}
Any idea ?
Thanks in advance
yc
 
 

Re:Calling a dll func from Java

yc wrote:
Quote

Hi,
I have a c dll (MyDll) stdcall fnc as follows.
stdcall int MyFnc(int FileNo, int IOType, unsigned char *
DataToWrite, unsigned char * DataRead);

I try to call from java code, but it gives me an error as follows.

Exception in thread "main" java.lang.UnsatisfiedLinkError: Myfunc
at X.MyFnc(Native Method)
at X.main(X.java:6)

That means that the Java process can't find the DLL in the system path.
Make sure that you add the appropriate directory to the PATH statement
or add -Djava.library.path=<dll_location>to the command line.
--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
www.datadevelopment.com/
NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
www.datadevelopment.com/papers/index.html
Please see Borland's newsgroup guidelines at
info.borland.com/newsgroups/guide.html
 

Re:Calling a dll func from Java

You cannot call any function from a dll. First you need to run javah to
create C or C++ header file from your compiled Java class. After that
you will need to implement that method to call native method with
correct parameters.
- Alexey.
Chris Lafferty wrote:
Quote
Kevin,

I am having a similar problem. I have done what you suggested below, adding
the -Djava.library.path= to the location of the .DLL. It seems more like it
cannot find the function inside the DLL as opposed to the DLL itself as
before I copied the DLL to that location, the error described the DLL name
instead of the funciton. I wrote a test DLL and was able to call it from
Delphi, so I know the methods are public and exported properly. However,
when I run from java, I get the same error as yc did. Here is my code and
error:

public class DLLBridge
{
public DLLBridge() {
}
public native void RprtMain();

static{
System.loadLibrary("Reports");
}

public static void main(String[] args) {
DLLBridge dbridge = new DLLBridge();
dbridge.RprtMain();
}
}

java.lang.UnsatisfiedLinkError: RprtMain
at com.util.DLLBridge.RprtMain(Native Method)
at com.util.DLLBridge.main(DLLBridge.java:21)
Exception in thread "main"

Is there something I'm missing? Thanks in advance,
Chris

"Kevin Dean [TeamB]" < XXXX@XXXXX.COM >wrote in message
news:xn0dyx2iu419bb001-kdean@www.teamb.com...

>yc wrote:
>
>
>>Hi,
>>I have a c dll (MyDll) stdcall fnc as follows.
>>stdcall int MyFnc(int FileNo, int IOType, unsigned char *
>>DataToWrite, unsigned char * DataRead);
>>
>>I try to call from java code, but it gives me an error as follows.
>>
>>Exception in thread "main" java.lang.UnsatisfiedLinkError: Myfunc
>>at X.MyFnc(Native Method)
>>at X.main(X.java:6)
>>
>
>That means that the Java process can't find the DLL in the system path.
>Make sure that you add the appropriate directory to the PATH statement
>or add -Djava.library.path=<dll_location>to the command line.
>
>--
>Kevin Dean [TeamB]
>Dolphin Data Development Ltd.
>www.datadevelopment.com/
>
 

{smallsort}

Re:Calling a dll func from Java

Kevin,
I am having a similar problem. I have done what you suggested below, adding
the -Djava.library.path= to the location of the .DLL. It seems more like it
cannot find the function inside the DLL as opposed to the DLL itself as
before I copied the DLL to that location, the error described the DLL name
instead of the funciton. I wrote a test DLL and was able to call it from
Delphi, so I know the methods are public and exported properly. However,
when I run from java, I get the same error as yc did. Here is my code and
error:
public class DLLBridge
{
public DLLBridge() {
}
public native void RprtMain();
static{
System.loadLibrary("Reports");
}
public static void main(String[] args) {
DLLBridge dbridge = new DLLBridge();
dbridge.RprtMain();
}
}
java.lang.UnsatisfiedLinkError: RprtMain
at com.util.DLLBridge.RprtMain(Native Method)
at com.util.DLLBridge.main(DLLBridge.java:21)
Exception in thread "main"
Is there something I'm missing? Thanks in advance,
Chris
"Kevin Dean [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
yc wrote:

>
>Hi,
>I have a c dll (MyDll) stdcall fnc as follows.
>stdcall int MyFnc(int FileNo, int IOType, unsigned char *
>DataToWrite, unsigned char * DataRead);
>
>I try to call from java code, but it gives me an error as follows.
>
>Exception in thread "main" java.lang.UnsatisfiedLinkError: Myfunc
>at X.MyFnc(Native Method)
>at X.main(X.java:6)
>

That means that the Java process can't find the DLL in the system path.
Make sure that you add the appropriate directory to the PATH statement
or add -Djava.library.path=<dll_location>to the command line.

--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
www.datadevelopment.com/

NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
www.datadevelopment.com/papers/index.html

Please see Borland's newsgroup guidelines at
info.borland.com/newsgroups/guide.html
 

Re:Calling a dll func from Java

Chris Lafferty wrote:
Quote
Kevin,

I am having a similar problem. I have done what you suggested below,
adding the -Djava.library.path= to the location of the .DLL. It
seems more like it cannot find the function inside the DLL as opposed
to the DLL itself as before I copied the DLL to that location, the
error described the DLL name instead of the funciton. I wrote a test
DLL and was able to call it from Delphi, so I know the methods are
public and exported properly. However, when I run from java, I get
the same error as yc did. Here is my code and error:

public class DLLBridge
{
public DLLBridge() {
}
public native void RprtMain();

static{
System.loadLibrary("Reports");
}

public static void main(String[] args) {
DLLBridge dbridge = new DLLBridge();
dbridge.RprtMain();
}
}

java.lang.UnsatisfiedLinkError: RprtMain
at com.util.DLLBridge.RprtMain(Native Method)
at com.util.DLLBridge.main(DLLBridge.java:21)
Exception in thread "main"

Is there something I'm missing? Thanks in advance,
Chris
The post from Alexey is likely to be more correct than the one I have
given you. I'm not intimately familiar with JNI and so gave the answer
that solves about 90% of the JNI questions we get. Have you done what
Alexey suggests?
--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
www.datadevelopment.com/
NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
www.datadevelopment.com/papers/index.html
Please see Borland's newsgroup guidelines at
info.borland.com/newsgroups/guide.html
 

Re:Calling a dll func from Java

Kevin/Alexey,
Thanks for the quick reply. He cleared up the basic issue of calling a dll
directly. First of all, I need to do some more reading up on JNI, I'm
obviously a little confused...but I guess what I am confused about is 1) is
the JNI C layer just an interface from Java to the DLL (when I ran javah, it
created a function called Java_DLLBridge_RprtMain which I'm hoping is just a
conduit from java to the DLL) or is 2) it actually asking for me to rewrite
a function that already works in a DLL but with parameters that Java can
communicate with. I'm guessing it's the 1st one, because it seems the second
alternative would defeat the whole purpose of using a fully
functioning/tested dll. Anyway, thanks for the info.
Chris
"Kevin Dean [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
Chris Lafferty wrote:

>Kevin,
>
>I am having a similar problem. I have done what you suggested below,
>adding the -Djava.library.path= to the location of the .DLL. It
>seems more like it cannot find the function inside the DLL as opposed
>to the DLL itself as before I copied the DLL to that location, the
>error described the DLL name instead of the funciton. I wrote a test
>DLL and was able to call it from Delphi, so I know the methods are
>public and exported properly. However, when I run from java, I get
>the same error as yc did. Here is my code and error:
>
>public class DLLBridge
>{
>public DLLBridge() {
>}
>public native void RprtMain();
>
>static{
>System.loadLibrary("Reports");
>}
>
>public static void main(String[] args) {
>DLLBridge dbridge = new DLLBridge();
>dbridge.RprtMain();
>}
>}
>
>java.lang.UnsatisfiedLinkError: RprtMain
>at com.util.DLLBridge.RprtMain(Native Method)
>at com.util.DLLBridge.main(DLLBridge.java:21)
>Exception in thread "main"
>
>Is there something I'm missing? Thanks in advance,
>Chris

The post from Alexey is likely to be more correct than the one I have
given you. I'm not intimately familiar with JNI and so gave the answer
that solves about 90% of the JNI questions we get. Have you done what
Alexey suggests?

--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
www.datadevelopment.com/

NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
www.datadevelopment.com/papers/index.html

Please see Borland's newsgroup guidelines at
info.borland.com/newsgroups/guide.html
 

Re:Calling a dll func from Java

On 2/26/2005 at 11:31:25 PM, Chris Lafferty wrote:
Quote
I guess what I am confused about is 1) is the JNI C layer just an
interface from Java to the DLL (when I ran javah, it created a function
called Java_DLLBridge_RprtMain which I'm hoping is just a conduit from
java to the DLL) or is 2) it actually asking for me to rewrite a function
that already works in a DLL but with parameters that Java can communicate
with. I'm guessing it's the 1st one, because it seems the second
alternative would defeat the whole purpose of using a fully
functioning/tested dll.
That is the signature of the C function that you need to write in order to
implement the RprtMain() method that you declared as "native" in your Java
DLLBridge class. Assuming you have an existing function in a DLL that you
are trying to call, you will typically just write this function to call
the other function. If the other function has parameters, you will
typically use JNI functions to get the parameter values and massage them
into the form required by that existing function, and do the reverse with
the function result.
Of course, there is no requirement that your native method call
pre-written C functions. You can implement your native method any way you
want to.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Calling a dll func from Java

John (and everyone else that replied on this thread),
That's what I was hoping and looking for. Thank you for the quick and
precise responses. That helps a bunch.
Thanks again,
Chris
"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
On 2/26/2005 at 11:31:25 PM, Chris Lafferty wrote:

>I guess what I am confused about is 1) is the JNI C layer just an
>interface from Java to the DLL (when I ran javah, it created a function
>called Java_DLLBridge_RprtMain which I'm hoping is just a conduit from
>java to the DLL) or is 2) it actually asking for me to rewrite a
function
>that already works in a DLL but with parameters that Java can
communicate
>with. I'm guessing it's the 1st one, because it seems the second
>alternative would defeat the whole purpose of using a fully
>functioning/tested dll.

That is the signature of the C function that you need to write in order to
implement the RprtMain() method that you declared as "native" in your Java
DLLBridge class. Assuming you have an existing function in a DLL that you
are trying to call, you will typically just write this function to call
the other function. If the other function has parameters, you will
typically use JNI functions to get the parameter values and massage them
into the form required by that existing function, and do the reverse with
the function result.

Of course, there is no requirement that your native method call
pre-written C functions. You can implement your native method any way you
want to.

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html