Forums Register Login

trouble with JNI native interface

+Pie Number of slices to send: Send
I am trying to get a simple native call to work. This is under FreeBSD, but the general concept is the same on any platform (replace your concept of shared libraries with your appropriate platform.)
If you want to help, please download my code, which is located temporarily here.
At first I hadn't set my library path, and got an error to that effect. I solved that problem. Now I set my library path to include the directory where the shared library is. But it is still unable to find the native method definition for the method "public native void pooBah()". The complete error message is:
<PRE>Exception in thread "main" java.lang.UnsatisfiedLinkError: pooBah
at JNITest.main(JNITest.java:10)
</PRE>
and that's all.
I think it may be something to do with package names or the method name. For some reason it can't find the method name specified. I don't know why. Can someone please help me? I've searched for similar problems and I can't think of anything I've overlooked.
I am using JDK 1.3.1.
Thanks

Geoffrey

------------------
Sun Certified Programmer for the Java 2 Platform

[This message has been edited by Geoffrey Falk (edited November 16, 2001).]
+Pie Number of slices to send: Send
Here are some details of my code. Please note that I have already used javah to generate the prototype for my native method, and I have set the LD_LIBRARY_PATH. Everything compiles fine, it just doesn't run. I don't know what else could possibly be the problem. This does not make any sense whatsoever.
<PRE>
JNITest.java ---------------
----------------------------
class JNITest {
static {
System.loadLibrary("native");
}
public native void pooBah();
public static void main(String args[]) {
new JNITest().pooBah();
}
}
-----------------------------
JNITest.h -------------------
-----------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNITest */
#ifndef _Included_JNITest
#define _Included_JNITest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNITest
* Method: pooBah
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JNITest_pooBah
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
-----------------------------
native.cc -------------------
-----------------------------
#include <jni.h>
#include "JNITest.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_JNITest_pooBah (JNIEnv *, jclass) {
printf("hello world!\n");
}
</PRE>

[This message has been edited by Geoffrey Falk (edited November 16, 2001).]
+Pie Number of slices to send: Send
Someone on Usenet helped me find the solution to my problem.
The answer is, you need to put extern "C" {} blocks around the method prototype in your .h file AND in your C++ source file. OTherwise C++ will mangle the function prototype and JNI can't find it.
Geoffrey

------------------
Sun Certified Programmer for the Java 2 Platform
+Pie Number of slices to send: Send
I have the code
blocks around my code in both the '.c' and '.h' file. But still I get the java.lang.UnsatisfiedLinkError error. Any idea why this happens? Where do I miss out?
+Pie Number of slices to send: Send
I've solved this myself. I just copied the .h file to .c file and started implementing it and forgot to remove the
macros from the implementation file. So, it generated the lib but without any functions. And that was the reason why I was able to load the DLL but not access the method.
Another problem I had was a wrong DLL version sitting in the java.library.path
-sudharsan
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1699 times.
Similar Threads
locating native files
Not able to find .dll file in my java path
compiling DLL for JNI use
unsatisfiedlinkerror while loading library file
JNI Dependent Libraries Not Found
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:33:43.