PDA

View Full Version : JNI: UnsatisfiedLinkError on Native Method Call


downplay
02-27-2009, 08:42 PM
PLATFORM: *Mac OS 10.5,6
TOOLS: *gcc 4.0.1, make, TextWrangler, vi
MACHINE: *PowerBook G4 1.5 Ghz
LANGUAGES: *C, Java 1.5.0_16



===================================================================================
PROBLEM DESCRIPTION:

I must be missing one simple step.

Java does is not able to link to a C method that I have defined and compiled.
However; I am certain java is finding my *.jnilib.

I took the gcc commands directly from a working XCode project and
modified them for my project location and file names. If XCode can do it, why
can't I?

I thought I had this solved!


===================================================================================
JAVA CODE

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class OfficeAppX11Native
{
public native int[] GetEventSummary();


// Load shared native code library
static
{
System.load("/Users/downplay/Desktop/OfficeAppJava/libOfficeAppX11Native.jnilib");
}

public OfficeAppX11Native()
{

}
}



===================================================================================
JAVA THAT CALLS THE WRAPPER CLASS FOR THE NATIVE CODE (ABOVE)

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class OfficeAppFrame extends JFrame
{
private Container myContentPane = null;

public OfficeAppFrame()
{
addWindowListener(new CloseWindowAndExit());

myContentPane = getContentPane();

myContentPane.setLayout(new GridBagLayout());

setSize(640,480);
pack();
setVisible(true);
}

class CloseWindowAndExit extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}

public static void main(String[] args)
{
int eventDataArray[];

OfficeAppFrame osFrame = new OfficeAppFrame ();

OfficeAppX11Native oaX11Native = new OfficeAppX11Native();

eventDataArray = oaX11Native.GetEventSummary();
}
}



===================================================================================
C CODE DEFINING THE NATIVE METHOD


#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <jni.h>

#include "OfficeAppX11Native.h"

JNIEXPORT jintArray JNICALL Java_OfficeAppX11Native_GetEventSummary
(JNIEnv *env, jobject jobj)
{
printf("In Native GetEventSummary!!!");
}



===================================================================================
C HEADER


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

#ifndef _Included_OfficeAppX11Native
#define _Included_OfficeAppX11Native
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: OfficeAppX11Native
* Method: GetEventSummary
* Signature: ()[I
*/
JNIEXPORT jintArray JNICALL Java_OfficeAppX11Native_GetEventSummary
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif



===================================================================================
GCC COMMANDS IN MAKEFILE

/Developer/usr/bin/gcc-4.0 -x c -arch ppc -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mfix-and-continue -mtune=G5 -mmacosx-version-min=10.5 -gdwarf-2 -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -I/Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/JavaVM.framework/Headers -c /Users/downplay/Desktop/OfficeAppJava/OfficeAppX11Native.c -o /Users/downplay/Desktop/OfficeAppJava/OfficeAppX11Native.o
/Developer/usr/bin/gcc-4.0 -arch ppc -dynamiclib -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -framework CoreFoundation -framework JavaVM -Wl,-single_module -compatibility_version 1 -current_version 1 -o /Users/downplay/Desktop/OfficeAppJava/libOfficeAppX11Native.jnilib



===================================================================================
CONSOLE OUTPUT

$ java OfficeAppFrame
Exception in thread "main" java.lang.UnsatisfiedLinkError: GetEventSummary
at OfficeAppX11Native.GetEventSummary(Native Method)
at OfficeAppFrame.main(OfficeAppFrame.java:41s)

downplay
02-28-2009, 12:11 AM
Problem Solved!!!

It seems to me like your second GCC command, which is supposed to take OfficeAppX11Native.o and put it in libOfficeAppX11Native.jnilib, fails to mention OfficeAppX11Native.o at all.

Replace


/Developer/usr/bin/gcc-4.0 -arch ppc -dynamiclib -isysroot
/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -framework
CoreFoundation -framework JavaVM -Wl,-single_module
-compatibility_version 1 -current_version 1 -o
/Users/downplay/Desktop/OfficeAppJava/libOfficeAppX11Native.jnilib

with


/Developer/usr/bin/gcc-4.0 -arch ppc -dynamiclib -isysroot
/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -framework
CoreFoundation -framework JavaVM -Wl,-single_module
-compatibility_version 1 -current_version 1 -o
/Users/downplay/Desktop/OfficeAppJava/libOfficeAppX11Native.jnilib /Users/downplay/Desktop/OfficeAppJava/OfficeAppX11Native.o

and see if that helps.