tags:

views:

734

answers:

1

Hi,

I created a program which uses JNI. I compiled it, generated header file, created a corresponding C program, and created dll for it.

I am running into an 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.(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.

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:

enter code here

System.load("D:/Program Files/Microsoft Visual Studio 9.0/VC/redist/x86/Microsoft.VC90.CRT/msvcr90.dll");
System.load("d:/examples/FirstJNIExample.dll");

I get a windows error:


Microsoft Visual C++ Runtime Library

Runtime Error!

Program: C:\Windows\system32\java.exe

R6034

An application has made an attempt to load the C runtime library incorrectly.
Please contact the application's support team for more information.


OK

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.(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.

+1  A: 

Looks like you need a manifest, or to switch to static linking, and/or manipulate PATH.

Almost any C++ code you write on Windows will make use of the MSVC runtime. By default, that code is linked dynamically. If you use dumpbin /imports on your JNI DLL you will see the dependency.

The JRE calls LoadLibrary and not LoadLibraryEx, so it isn't good enough to have the dependency sitting right next to the JNI library. You have to have the directory containing the MSVC runtime DLL in PATH. Or, you need to link it statically, but I'm not sure this is possible with MSVC90. Or, you need to manifest it.

bmargulies