I've already found a lot of things and questions about UnsatisfiedLinkError in combination with JNI but none of them could help me out though. I'm trying to hide a window using Java, so I need 3 WinAPI functions.
I have the following Java source:
package hide.window;
public class HideWindow {
public native static int hideWindow(char[] windowTitle);
static {
System.loadLibrary("hideWindow");
}
public static void main(String[] args) {
System.out.println("loadLibrary was successful");
System.out.println(hideWindow("Session Window - Windows Internet Explorer".toCharArray()));
}
}
Then I have my hide_window_HideWindow.h generated by javah. There's one thing I had to change, because it didn't find the jni.h file in the library path so I just put it into my project and included it the following way:
#include "jni.h"
May this cause my Error?? The whole file:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class hide_window_HideWindow */
#ifndef _Included_hide_window_HideWindow
#define _Included_hide_window_HideWindow
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: hide_window_HideWindow
* Method: hideWindow
* Signature: ([C)I
*/
JNIEXPORT jint JNICALL Java_hide_window_HideWindow_hideWindow
(JNIEnv *, jobject, jcharArray);
#ifdef __cplusplus
}
#endif
#endif
The code I compiled to my DLL looks as follows:
#include <windows.h>
#include "hide_window_HideWindow.h"
using namespace std;
JNIEXPORT jint JNICALL Java_hide_window_HideWindow_hideWindow(char windowTitle[20])
{
HWND windowHandle = FindWindowA(NULL, windowTitle);
if (windowHandle){
if (IsWindowVisible(windowHandle)){
ShowWindow(windowHandle, 0);
return 0;
}
else{
ShowWindow(windowHandle, 1);
return 1;
}
}
return -1;
}
There's no difference between a call from eclipse and the following call via cmd (Path is correct):
java -Djava.library.path=.. hide.window.HideWindow
The resulting output looks like that:
loadLibrary was successful Exception in thread "main" java.lang.UnsatisfiedLinkError: hide.window.HideWindow.hideWindow([C)I at hide.window.HideWindow.hideWindow(Native Method) at hide.window.HideWindow.main(HideWindow.java:12)
That indicates to me that the Java program is able to load the DLL but it fails at the function call. I use Eclipse as Java and Codeblocks as C++ IDE, and I have already added the following to the mingw build options.
-Wl,--kill-at
I hope this question is not too stupid and thank you in advance, Tom
Look at the prototype:
JNIEXPORT jint JNICALL Java_hide_window_HideWindow_hideWindow
(JNIEnv *, jobject, jcharArray);
and your implementation:
JNIEXPORT jint JNICALL Java_hide_window_HideWindow_hideWindow
(char windowTitle[20])
Of course there's an UnsatisfiedLinkError
because you didn't implement the correct function.
So you would need something like:
JNIEXPORT jint JNICALL Java_hide_window_HideWindow_hideWindow
(JNIEnv* env, jobject thiz, jcharArray windowTitle)
And to get the chars out of the array have a look at GetPrimitiveArrayCritial
or GetCharArrayRegion
.
The generated header also look suspicious to me, because you defined the Java method to be static
but the generated prototype wants to pass a jobject
... static methods need jclass
(because there is no instance object for static method calls):
JNIEXPORT jint JNICALL Java_hide_window_HideWindow_hideWindow
(JNIEnv* env, jclass clazz, jcharArray windowTitle)
Another thing: Java's char
(jchar
) is an unsigned int16
, C/C++ char
is signed or unsigned int8
. So it might be better to pass a String
(jstring
) and get the contents as an UTF8 string using GetStringUTFChars
.