• Post Reply Bookmark Topic Watch Topic
  • New Topic

compiling DLL for JNI use  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've been trying for some time to get this to work. I've basically followed the JNI tutorial on Sun's web site but every time I try to run the sample application I get "UnsatisfiedLinkError". But the problem is not that the DLL is not getting loaded, it just can't find the method in the DLL. I have tried compiling with VC as well as with Borland but I always get the same error. IS there any tutorial that shows how to do this easily?

Thanks
 
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't get much easier than the Java Tutorial. Direct from Step 6:

If you see an exception like the following, then you don't have your library path set up correctly.

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)


How to set the library path is covered here.
 
Matt Rossner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't the problem I have my library path set up properly, I'm running like:

java -Djava.library.path=<path_to_dll> MyClass

if "path_to_dll" is wrong then I get this exception:

The java class could not be loaded. java.lang.UnsatisfiedLinkError: Can't find library Hello2Jni (Hello2Jni.dll) in sun.boot.library.path or java.library.path
sun.boot.library.path=G:\RAD6\eclipse\jre\bin
java.library.path=F:\Workspaces\debugUtils\dll\HelloJNI\Hello2Jni\Debu

If I correct the path, I get this instead:

Exception in thread "main" java.lang.UnsatisfiedLinkError: helloJni
at HelloJNI.main(HelloJNI.java:14)

Where helloJni is the name of the method in the dll I am trying to call.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so then the problem is most likely just that you've named the native method incorrectly, or provided the wrong compiler-specific declarations. Make sure you use the headers generated by javah, and make sure you copy the declaration from the header and use that to define your method. Make sure your method is named, e.g., Java_thepackage_HelloJNI_helloJNI .
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do have some code in your helloJni, don't you? VC has a nice way of optimizing your code. It removes empty functions

If you want to check whther your function is exported from the DLL or not, do this

1) Open command prompt
2) Change your current directory to your path to dll
3) run this command :- dumpbin /exports hellojni.dll
4) this should print the mangled names of all the functions that are exported by the DLL

First check if helloJNI is there in the list or not. If it's not there then that means that you haven't marked the function to be exported correctly, or for some reason the compiler is'nt compiling it. If it's there then that means that the signature of the function is differrent from what Java expects.
For example, running dumpbin on my native dll returns this



I would suggest that you try some other samples and work from there, but if you really want to get into the nitty-gritty of mangled names, you will have to google it (sorry i tried, but I can't spend too much time on it)
 
Matt Rossner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, well I was using the h file from javah, but this dumpbin utility sounds promising, I'll play with that a bit and see if I can find out why it's not working.

Thanks for your help
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about the cpp file? Make sure that the signature of the function in .h matches the one in cpp

So, if your .h has


you .cpp should have


Also make sure you import jni.h in your .h file. If you used javah, then that should be there. Also, make sure you have the correct jni-md.h in your include path. If you have the wrong one, your method signatures will all get screwed up and neither VC nor Java compiler will complain about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!