首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载JNI Ubuntu失败

加载JNI Ubuntu失败
EN

Stack Overflow用户
提问于 2015-09-15 18:51:11
回答 1查看 252关注 0票数 0

我正在尝试加载JNI库并运行下面的程序,但是我得到了下面的错误

代码语言:javascript
复制
Exception in thread "main" java.lang.UnsatisfiedLinkError: no JNIDemo      
in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at demo.JNIWrapper.<clinit>(JNIWrapper.java:10)

下面是我的Java,C,命令

步骤1:Java代码:出现在路径(/documents/JNIDemoProject/src/main/java/demo)处的Eclipse项目中

代码语言:javascript
复制
package demo;

public class JNIWrapper {

static{


    //System.load("/home/arpit/Documents/JNI/libJNIDemo.so");

    System.loadLibrary("JNIDemo");
}


public native int multiply(int a,int b);


public static void main(String args[]){

    try{

    JNIWrapper jni=new JNIWrapper();
    int result=jni.multiply(7, 8);
    System.out.println("Result is "+result);
    }catch(Exception e){
        e.printStackTrace();
    }

}

}

步骤2:创建了一个名为demo_JNIWrapper的主文件(注意名称是demo_JNIWrapper,因为我必须从/documents/JNIDemoProject/src/ .h /java运行javah命令)

代码语言:javascript
复制
 /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class demo_JNIWrapper */

#ifndef _Included_demo_JNIWrapper
#define _Included_demo_JNIWrapper
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     demo_JNIWrapper
 * Method:    multiply
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_demo_JNIWrapper_multiply
  (JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif


#endif

步骤3:创建一个C文件

代码语言:javascript
复制
#include<stdio.h>
#include <jni.h>
#include "demo_JNIWrapper.h"

JNIEXPORT jint JNICALL Java_demo_JNIWrapper_multiply
 (JNIEnv *env, jobject jobj, jint a, jint b){

int result=a*b;
return result;
}

步骤4:创建名为libJNIDemo.so的库文件

步骤5:所有三个文件(libJNIDemo.so、demo_JNIWrapper.h、HelloJNI.c)都位于文件夹/users/documents/JNI中

步骤6:我将其导出到库路径export LD_LIBRARY_PATH="/users/documents/JNI"

Step7:当我运行java程序时,它给出了上面的错误。

注意:当我使用路径直接加载libJniDemo.so文件时,我的程序运行正常

代码语言:javascript
复制
static{


    System.load("/users/documents/JNI/libJNIDemo.so");

    //System.loadLibrary("JNIDemo");
}

有没有人能建议

EN

回答 1

Stack Overflow用户

发布于 2015-09-16 02:52:41

Java在Linux (和其他Unix或类似Unix的操作系统)上使用dlopen()来查找本机库。从Linux dlopen() man page

代码语言:javascript
复制
   If filename is NULL, then the returned handle is for the main
   program.  If filename contains a slash ("/"), then it is interpreted
   as a (relative or absolute) pathname.  Otherwise, the dynamic linker
   searches for the object as follows (see ld.so(8) for further
   details):

   o   (ELF only) If the executable file for the calling program
       contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
       then the directories listed in the DT_RPATH tag are searched.

   o   If, at the time that the program was started, the environment
       variable LD_LIBRARY_PATH was defined to contain a colon-separated
       list of directories, then these are searched.  (As a security
       measure, this variable is ignored for set-user-ID and set-group-
       ID programs.)

   o   (ELF only) If the executable file for the calling program
       contains a DT_RUNPATH tag, then the directories listed in that
       tag are searched.

   o   The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is
       checked to see whether it contains an entry for filename.

   o   The directories /lib and /usr/lib are searched (in that order).

如果可以,在strace下运行您的Java进程,跟踪open调用,并查看JVM在哪里查找您的本机库,以及它到底在查找什么。

了解了dlopen()的工作原理,以及来自strace的信息,您应该能够准确地确定发生了什么。

需要注意的一件事是:您可能正在创建一个64位的本地库,但却运行着一个32位的JVM。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32584141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档