JNI link error: A dynamic link library (DLL) initialization routine failed
866026Jun 1 2011 — edited Nov 16 2011Can 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 _Java_HelloWorld_helloWorld@8
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, "_Java_HelloWorld_helloWorld@8");
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,