简体   繁体   中英

Eclipse JNI UnsatisfiedLinkError when compiling in terminal works

I've been trying all day to get a basic JNI project to work in eclipse. I just got it working outside of eclipse (see this thread UnsatisfiedLinkError in JNI Code ), and as soon as I transfered it to eclipse, I got an

Exception in thread "main" java.lang.UnsatisfiedLinkError: no HPAProgram in java.library.path

For line 10 in my java file (the load library line). I have no idea what's going on. What could be different between Eclipse and compiling on command line? I'm using OS X, and I am able to generate my .dylib, so that's not the issue. Here's my code:

makefile:

# Define a variable for classpath
CLASS_PATH = ../bin

# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)

all: libhpaprogram.dylib

# $@ matches the target, $< matches the first dependancy
libhpaprogram.dylib:
    cc -v -c -fPIC -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/ HPAProgram.c++ -o libhpaprogram.o
    libtool -dynamic -lSystem libhpaprogram.o -o libhpaprogram.dylib

HPAProgram.h : HPAProgram.class
    javah HPAProgram

clean:
    rm HPAProgram.h libhpaprogram.o libhpaprogram.dylib

HPAProgram.c++

/*
 * HPAProgram.c++
 *
 *  Created on: Feb 4, 2014
 *      Author: zalbhathena
 */

//#include <jni.h>
#include <stdio.h>
#include "HPAProgram.h"

JNIEXPORT void JNICALL Java_HPAProgram_sayHello (JNIEnv *env, jobject obj) {
   printf("Hello World!\n");
}

HPAProgram.java:

public class HPAProgram {

    public native void sayHello();
    public static void main(String[] args) {
        System.loadLibrary("HPAProgram");
        HPAProgram s = new HPAProgram();
        s.sayHello();

    }
}

HPAProgram.h

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

#ifndef _Included_HPAProgram
#define _Included_HPAProgram
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HPAProgram
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HPAProgram_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

I figured it out! I found it in this thread How to debug a java system.loadlibrary error in linux?

"As far as I know the Eclipse doesn't use the LD_LIBRARY_PATH. The easiest way to set up the right native library path is to go to Project properties -> Java Build Path -> Libraries Then expand either the JRE System Library entry or (if available) the Jar File that uses you native Library, choose "Native Library Location" then click "Edit..." and choose the folder your libraries are in. Actually it does set the -Djava.library.path variable so you have to include this in your command line if you start your program from outside eclipse."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM