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

Greg Guerin wrote

The following works perfectly for me.

I did exactly what you showed, but it didn't work for me. The output is below, along with answers to your later question, and Mike Hall's question.

First of all, since your java code called System.loadLibrary() in the static block before calling main, I moved the call to main so I could run some tests first. The tests verify that the library is on java.library.path. Here's my

File ----- import; import; import java.util.*;

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

final String baseLibName = "Piddy";
final String libname = "lib" + baseLibName + ".jnilib";
final StringTokenizer stk = new StringTokenizer(libPath, ":");
while (stk.hasMoreTokens()) {
final String path = stk.nextToken();
fileExists(path, libname);
System.out.println("Loading Library");
System.loadLibrary( "Piddy" );

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

private static void fileExists(String dir, String libname) {
File libfile = new File(dir, libname);
String libFileName = libfile.toString();
System.err.println("Library exists = " + libfile.exists());
if (libfile.exists()) {
try {
String absolutePath = libfile.getCanonicalPath();
if (! absolutePath.equals(libFileName)) {
} catch (IOException e) {
e.printStackTrace(); // shouldn't happen
if (libfile.exists()) {
Calendar timeStamp = Calendar.getInstance();
System.err.println(" Modified at " + timeStamp.getTime());

  private static native int pid();

private static native int ppid(); }


My Piddy.c is exactly the same as yours. Here's my command-line output:

(Please note that lsf is aliased to ls -F. Also, I include the output of the file and otool commands at the end.)

Aside from my change to, everything follows your example exactly.

miguel-munozs-mac-pro:Piddy miguel$ rm Piddy.h
miguel-munozs-mac-pro:Piddy miguel$ lsf
miguel-munozs-mac-pro:Piddy miguel$ javac
miguel-munozs-mac-pro:Piddy miguel$ javah -jni Piddy
miguel-munozs-mac-pro:Piddy miguel$ gcc -x c -I/System/Library/ Frameworks/JavaVM.framework/Headers -c Piddy.c -o Piddy.o
miguel-munozs-mac-pro:Piddy miguel$ gcc -dynamiclib -o libPiddy.jnilib Piddy.o
miguel-munozs-mac-pro:Piddy miguel$ java Piddy
java.library.path = .:/Library/Java/Extensions:/System/Library/Java/ Extensions:/usr/lib/java
Library exists = true
Modified at Tue Jun 16 13:22:29 PDT 2009

Library exists = false

Library exists = false

Library exists = false

Loading Library
Exception in thread "main" java.lang.UnsatisfiedLinkError: /Users/ miguel/DevProjects/Piddy/libPiddy.jnilib:
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(
at java.lang.ClassLoader.loadLibrary(
at java.lang.Runtime.loadLibrary0(
at java.lang.System.loadLibrary(
at Piddy.main(
miguel-munozs-mac-pro:Piddy miguel$ file libPiddy.jnilib
libPiddy.jnilib: Mach-O dynamically linked shared library i386
miguel-munozs-mac-pro:Piddy miguel$ otool -L libPiddy.jnilib
libPiddy.jnilib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)
miguel-munozs-mac-pro:Piddy miguel$ lsf
Piddy.c Piddy.class Piddy.h Piddy.o libPiddy.jnilib*
miguel-munozs-mac-pro:Piddy miguel$

-- Miguel Muñoz _______________________________________________
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.

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.