TreeTree
TreeTree

Reputation: 3230

Java cannot load shared object library with JNI on OSX

I'm trying to use JNI to join Java and C in the simplest way on my friend's 64-bit OSX and I'm getting this error. Here's everything involved:

test.java

public class test {

    static {System.loadLibrary ("test");}

    native void aaa ();

    public static void main (String [] args) {
        new test ();
    }

    public test () {
        aaa ();
    }
}

test.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class test */

#ifndef _Included_test
#define _Included_test
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     test
 * Method:    aaa
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_test_aaa
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

test.c

#include <stdio.h>
#include <jni.h>
#include "test.h"

JNIEXPORT void JNICALL Java_test_aaa
  (JNIEnv *env, jobject obj) {
    printf ("AWD");
}

makefile

CC          =   gcc
CFLAGS      =   -Wall -ansi -pedantic -g3

default :
    javac test.java
    javah -jni test
    gcc -c test.c -o test.o -I${HOME}/../../System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/
    gcc -shared -Wl,-install_name,libtest.so -o libtest.so test.o

I had to use -install_name instead of -soname because I read that OSX doesn't have -soname like Linux.

Then I do

export LD_LIBRARY_PATH=.
java test

and I get

Exception in thread "main" java.lang.UnsatisfiedLinkError: no test in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1856)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at test.<clinit>(test.java:2)

So I really don't know what the problem is. I copied all the files onto Linux and switched -install_name to -soname and changed the paths to jni.h and it works just fine.

Upvotes: 4

Views: 4453

Answers (1)

slugonamission
slugonamission

Reputation: 9632

On OS X, JNI looks for libraries with the extension .jnilib, or with the standard OS X shared library extension of .dylib.

Source: http://developer.apple.com/library/mac/#documentation/Java/Conceptual/Java14Development/05-CoreJavaAPIs/CoreJavaAPIs.html

Upvotes: 6

Related Questions