本文介绍了Linux上的JNI问题:无法打开共享对象文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了这个问题,尝试了建议的修复程序,但到目前为止对我而言没有成功.我有一定的Java经验,但是JNI很久以前了,虽然在Linux上从来没有...

I've seen this question on here, tried the proposed fixes, but no success so far for me. I have quite some Java experience, but JNI is a long time ago, never did it on Linux though...

我正在尝试让一个简单的HelloWorld JNI应用程序在Linux上运行.

I'm trying to get a simple HelloWorld JNI app running on Linux.

小型java文件:

class HelloWorld {

    private native void print();

    public static void main(String[] args){
        new HelloWorld().print();
    }

    static {
        System.out.println(System.getProperty("java.library.path"));
        System.loadLibrary("HelloWorld");
    }

}

小型C文件:

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"

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

通过以下方式编译C文件:

compiled the C file by:

gcc -shared -Wall -fPIC HelloWorld.c -I/usr/lib/gcc/x86_64-redhat-linux/3.4.3/include/ -o libHelloWorld.so

通过以下方式运行应用程序:

Run the app by:

java HelloWorld

java -Djava.library.path=/home/nxp40954/jnitesting/. HelloWorld

但是得到一个不好:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/nxp40954/jnitesting/libHelloWorld.so: /home/nxp40954/jnitesting/libHelloWorld.so: cannot open shared object file: No such file or directory

奇怪,因为实际上有一个/home/nxp40954/jnitesting/libHelloWorld.so文件.

Strange, because there is actually a /home/nxp40954/jnitesting/libHelloWorld.so file.

有人知道吗?

推荐答案

以这种方式执行:

export LD_LIBRARY_PATH=.
java HelloWorld

无法加载.so文件时,引发java.lang.UnsatisfiedLinkError.LD_LIBRARY_PATH变量指出了寻找* .so文件的额外位置.

The java.lang.UnsatisfiedLinkError is thrown when the .so file cannot be loaded.The LD_LIBRARY_PATH variable points extra location to look for the *.so files.

我在使用Sun Java的32位ubuntu上.我是这样编译的:

I'm on 32bit ubuntu with sun java. I was compiling this way:

gcc -shared -Wall -fPIC HelloWorld.c -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux -o libHelloWorld.so

这篇关于Linux上的JNI问题:无法打开共享对象文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:33