Linux 上的 JNI 问题:无法打开共享对象文件 [英] JNI issue on Linux: cannot open shared object file

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

问题描述

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...

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

Small java file:

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");
    }

}

Small C file:

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

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

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

or

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

But no good, getting a:

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

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

Does anyone have a clue?

解决方案

execute this way:

export LD_LIBRARY_PATH=.
java HelloWorld

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.

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 问题:无法打开共享对象文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆