Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: UnsatisfiedLinkError after building a JNI Library
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UnsatisfiedLinkError after building a JNI Library



Miguel Munoz wrote:

gcc -g -dynamiclib -framework JavaVM src/cReverse.o src/jni.o -o out/production/Cjni/libCjni.jnilib
....
I'm calling System.loadLibrary("CjniLib") with a library file named "libCjniLib.jnilib", although when I try calling System.load (basePath + "libCjniLib.jnilib"), ...

These are inconsistent.

The output from gcc is named "libCjni.jnilib". For System.loadLibrary (), you would use "Cjni" to load this lib, not "CjniLib".

There is a similar problem with the path you pass to System.load(). The filename simply doesn't match what gcc is producing.

Maybe you renamed gcc's output file to "libCjniLib.jnilib". But if you didn't, you're using the wrong lib name in the Java code. If that's the problem, I recommend naming the lib something more consistent with its purpose and less repetitive. Even "Fubar" would be better than what you have now.


The following works perfectly for me.

  -- GG


File Piddy.java ----- public class Piddy { public static void main( String[] args ) { System.out.println( "java.library.path = " + System.getProperty( "java.library.path" ) );

    System.out.println( "pid = " + pid() );
    System.out.println( "ppid = " + ppid() );
  }

  private static native int pid();

  private static native int ppid();

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


File Piddy.c ----- #include <sys/types.h> #include <unistd.h>

#include "Piddy.h"

JNIEXPORT jint JNICALL
Java_Piddy_pid(JNIEnv * env, jclass cl)
{  return getpid();  }

JNIEXPORT jint JNICALL
Java_Piddy_ppid(JNIEnv * env, jclass cl)
{  return getppid();  }


Commands to build and run: ----- javac Piddy.java

javah -jni Piddy

gcc -x c \
  -I/System/Library/Frameworks/JavaVM.framework/Headers \
  -c Piddy.c -o Piddy.o

gcc -dynamiclib -o libPiddy.jnilib Piddy.o

java Piddy

file lib*

## end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.