Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble with JNI native interface

 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic