Forum Stats

  • 3,733,083 Users
  • 2,246,691 Discussions
  • 7,856,504 Comments

Discussions

JNI link error: A dynamic link library (DLL) initialization routine failed

866026
866026 Member Posts: 1
edited November 2011 in Java Native Interface (JNI)
Can any help help with a JNI question?

I"m sure the DLL is being found because if I set java.library.path to incorrect value I get this error instead: no HelloWorld in java.library.path

HelloWorld.java::
----------------------------------------------------
public class HelloWorld {
static void runTest() {
System.loadLibrary("HelloWorld");
helloWorld();
}
native public static void helloWorld();

public static void main(String[] args) {
HelloWorld.runTest();
}
}

HelloWorld.cpp
----------------------------------------------------
#include "HelloWorld.h"
#include <jni.h>
#include <iostream>

extern "C" JNIEXPORT void JNICALL Java_HelloWorld_helloWorld(JNIEnv *, jclass)
{
std::cout << "Java_HelloWorld_helloWorld" << std::endl;
}

I'm using VC express 2008
----------------------------------------------------

% c:\Progra~1\Java\jdk1.6.0_21\bin\javah -classpath ../bin HelloWorld
% cl /c /DWIN32 /GF /GR /EHa /EHc /TP /W2 /LD /MD HelloWorld.cpp
% cl /LD /MD HelloWorld.obj /FeHelloWorld.dll

Test output
----------------------------------------------------
% java -cp ../bin HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: HelloWorld.dll: A dynamic link 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.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at HelloWorld.runTest(HelloWorld.java:4)
at HelloWorld.main(HelloWorld.java:10)

Symbols in DLL.
----------------------------------------------------
% dumpbin /exports *.dll
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

Dump of file HelloWorld.dll

File Type: DLL

Section contains the following exports for HelloWorld.dll

00000000 characteristics
4DE6BD9D time date stamp Wed Jun 01 15:30:53 2011
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 00001000 [email protected]

Removing the extern "C" has no effect. Symbol looks same according to dumpbin.

C++ Main to test DLL.
----------------------------------------------------

#include "HelloWorld.h"
#include <iostream>
#define WIN32_LEAN_AND_MEAN

using namespace std;

#include <windows.h>

typedef void JNICALL Java_HelloWorld_helloWorld_PROC(JNIEnv *, jclass);

int main(int ac, char *av[])
{
HMODULE inst = LoadLibrary("HelloWorld");

cout << "inst:" << inst << endl;
if ( inst == 0 ) return 0;
void * mth = GetProcAddress( inst, "[email protected]");
cout << "mth:" << mth << endl;

if ( mth == 0 ) return 0;

Java_HelloWorld_helloWorld_PROC * proc = (Java_HelloWorld_helloWorld_PROC*)mth;

JNIEnv * envP = 0;
jclass clazz = 0;
(*proc)(envP,clazz);

FreeLibrary(inst);
}

Output is:
----------------------------------------------------
inst:10000000
mth:10001000
Java_HelloWorld_helloWorld


Thanks,

Answers

  • Michaelt-Oracle
    Michaelt-Oracle Member Posts: 15
    Try removing the /MD linker option. If that works for you then you and are having the same problem. If I link with /MD I get "A dynamic link library (DLL) initialization routine failed". If I remove /MD then the Jvm can load the library. Unfortunately I absolutely must link with /MD.
  • Michaelt-Oracle
    Michaelt-Oracle Member Posts: 15
    edited November 2011
    Are you by chance using a 32bit Jvm and 32bit compiler on a 64bit windows platform? From Microsoft's description of /MD...

    "Causes your application to use the multithread- and DLL-specific version of the run-time library. Defines _MT and _DLL and causes the compiler to place the library name MSVCRT.lib into the .obj file.
    Applications compiled with this option are statically linked to MSVCRT.lib. This library provides a layer of code that allows the linker to resolve external references. The actual working code is contained in MSVCR100.DLL, which must be available at run time to applications linked with MSVCRT.lib."

    In my case I suspect the Jvm is attempting to load a 64bit version of the c run-time into a 32bit Jvm, but that's just a hunch at this point.

    Edited by: user709182 on Nov 16, 2011 2:25 PM
  • EJP
    EJP Member Posts: 32,919
    You don't have any evidence for that guess.
This discussion has been closed.