【问题标题】:Eclipse JNI UnsatisfiedLinkError when compiling in terminal works在终端中编译时出现 Eclipse JNI UnsatisfiedLinkError
【发布时间】:2014-03-01 11:08:52
【问题描述】:

我整天都在努力让一个基本的 JNI 项目在 Eclipse 中工作。我刚刚让它在 Eclipse 之外工作(参见这个线程 UnsatisfiedLinkError in JNI Code),当我将它转移到 Eclipse 时,我得到了一个

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

对于我的 java 文件中的第 10 行(加载库行)。我不知道是怎么回事。 Eclipse 和命令行编译有什么不同?我使用的是 OS X,并且能够生成我的 .dylib,所以这不是问题。这是我的代码:

制作文件:

# 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

【问题讨论】:

    标签: java c++ eclipse gcc java-native-interface


    【解决方案1】:

    我想通了!我在这个帖子里找到了它How to debug a java system.loadlibrary error in linux?

    “据我所知,Eclipse 不使用 LD_LIBRARY_PATH。设置正确的本地库路径的最简单方法是转到项目属性 -> Java 构建路径 -> 库,然后展开 JRE 系统库条目或(如果可用)使用您本机库的 Jar 文件,选择“本机库位置”,然后单击“编辑...”并选择您的库所在的文件夹。实际上它确实设置了 -Djava.library.path 变量因此,如果您从 Eclipse 外部启动程序,则必须将其包含在命令行中。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多