Java cannot load shared object library using JNI on OSX

I am trying to use JNI to join Java and C in the easiest way on my friend's 64-bit OS machine and I am getting this error. Everything is involved here:

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 does.

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 files on Linux and changed the -install_name to -soname and changed the paths to jni.h and it works fine.

+3


source to share


1 answer


On OS X, JNI looks for libraries with an extension .jnilib

or standard extension of the OS X shared library .dylib

.



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

+5


source







All Articles