This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes UnsatisfiedLinkError in my first JNI program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "UnsatisfiedLinkError in my first JNI program" Watch "UnsatisfiedLinkError in my first JNI program" New topic
Author

UnsatisfiedLinkError in my first JNI program

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi,

I created a program which uses JNI. I compiled it, generated header file, created a corresponding C program, and created dll for it.
Now I am placing the dll in the folder where my Java program is e.g. my class com.ankur.FirstJNIExample is in d:\examples folder so I am placing this dll into d:\examples folder.

But when I run it I get following exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no FirstJNIExample.dll in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at com.ankur.FirstJNIExample.<clinit>(FirstJNIExample.java:8)
Could not find the main class: com.ankur.FirstJNIExample. Program will exit.

I read here that if you place your dll in same folder then you should be okay. Why it's not picking up my dll then?

Now to get going, I changed my code to this:


It doesn't give above exception as it finds the dll now. But now I am running into another exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\examples\FirstJNIExample.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at com.ankur.FirstJNIExample.<clinit>(FirstJNIExample.java:9)
Could not find the main class: com.ankur.FirstJNIExample. Program will exit.

I don't know which all dlls my FirstJNIExample.dll is dependent on. How can I know that? The dll file is unreadable in textpad.

I am not doing much in C program either:


What can be the issue?

Thanks.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I saw in Dependency Walker that my dll is dependent on following dlls:
msvcr90.dll
kernel32.dll
and this kernel32.dll is dependent on:
ntdll.dll

I also see an error in Dependency Walker for msvcr90.dll that system can't find the file specified.

This dll is present in my system at this location: D:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT

When I thought of loading it on my own in code like this:
view plaincopy to clipboardprint?



I get a windows error:



And this exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\Program Files\Micr
osoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\msvcr90.dll: A dynamiclink library (DLL) initialization routine failed
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at com.ankur.FirstJNIExample.<clinit>(FirstJNIExample.java:10)
Could not find the main class: com.ankur.FirstJNIExample. Program will exit.

I am just lost. So many questions:
Why my dll is dependent on msvcr90.dll?
How do I load this dll?

I never worked with dlls before.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: UnsatisfiedLinkError in my first JNI program
 
Similar Threads
Using JNI to call MS Windows API functions
Cannot use Java 3D API with Eclipse? Help!
Access dll functions in java
JNI Dependent Libraries Not Found
calling C from java