Ok....newbie to JNI here. I know there are quite a few posts like this here..but everyone seems to be on XP/Vista, nothing for MAC/Unix
This is my problem:
I have just created a small bit of code to test the JNI and it fails rather miserably with an unhelpful UnsatisfiedLinkError message. So onto the code and my observations:
This is my HelloJNI.java
class HelloJNI {
private native static void print();
public static void main(String[] args) {
print();
}
static {System.out.println ( System.getProperty( "java.library.path" ) );System.loadLibrary("HelloJNI");System.out.println("Done");}
}
Compiled successfully to HelloJNI.class
This is my HelloJNI.h header file as generated by javah -jni HelloJNI
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloJNI */
#ifndef _Included_HelloJNI
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloJNI
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloJNI_print
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
Here is the HelloJNI.cpp file:
#include "HelloJNI.h"
#include <iostream>
using namespace std;
JNIEXPORT void JNICALL Java_HelloJNI_print (JNIEnv *, jclass) {
cout<<"Aye hello from JNI";
}
Now I have gone about this several ways....but in general the correct way to go about compiling this seems to have been to generate a .o file and then generate a file named libHelloJNI.jnilib. For all you doubters out there, this is the CORRECT name of the file, MAC OSX expects a library with the lib prefix and the .jnilib suffix. I can prove this to you in a second...but even the official technical notes I have come across on the web have told me that I need this special name on OSX in order for java to find my library. But this command works successfully:
g++ -O3 -DNDEBUG -feliminate-unused-debug-symbols -fPIC -dynamiclib -o libHelloJNI.jnilib -I\/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Headers HelloJNI.cpp
When I next run "java HelloJNI" I get this:
.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/javaException in thread "main" java.lang.UnsatisfiedLinkError: /libHelloJNI.jnilib:
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1822)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1739)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at HelloJNI.<clinit>(HelloJNI.java:6)
HOWEVER this does NOT mean it hasn't found the file...because if I delete the libHelloJNI.jnilib from the root directory and run "java HelloJNI" I get this:
.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/javaException in thread "main" java.lang.UnsatisfiedLinkError: no HelloJNI in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1753)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at HelloJNI.<clinit>(HelloJNI.java:6)
So, this is kinda proof that my library file needs to be called libHelloJNI.jnilib, as the error message seems to change when it is present. FYI yes I am running this from the root of my drive, and I also have a file called libHelloJNI.so in the same directory, which java could seem to care less about.
Any help would be great, this is a very frustrating problem.
EDIT:
Quickly some information I forgot to include:
OS: 10.5.4 OSX
JAVA: java -version produces:
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13-120)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_05-b13-52, mixed mode)
g++ ---version:
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5488)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Processor: 2.1 GHz Intel Core 2 Duo
Type of Computer: 3rd or 4th generation macbook
Edited by: LordDelta on Sep 20, 2008 10:44 AM