如何在 linux 上为 JNI 应用程序编译动态库? [英] How to compile dynamic library for a JNI application on linux?

查看:28
本文介绍了如何在 linux 上为 JNI 应用程序编译动态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Ubuntu 10.10

这就是我所做的.

Hello.java:

class 你好{公共本机无效 sayHello();静态 { System.loadLibrary("hellolib");}公共静态无效主(字符串 [] args){你好 h = new Hello();h.sayHello();}}

然后我运行了以下命令:

dierre@cox:~/Scrivania/provajni$ javac Hello.javadierre@cox:~/Scrivania/provajni$ javah -jni 你好

我已经获得了 Hello.classHello.h.

Hello.h:

/* 不要编辑这个文件 - 它是机器生成的 */#include /* 类 Hello 的标题 */#ifndef _Included_Hello#define _Included_Hello#ifdef __cplusplus外部C"{#万一/** 班级:你好* 方法:sayHello* 签名:()V*/JNIEXPORT void JNICALL Java_Hello_sayHello(JNIEnv *, 作业);#ifdef __cplusplus}#万一#万一

然后我创建了Hello.cpp:

#include #include "Hello.h"#include 使用命名空间标准;JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {cout<<你好世界!"<<结束;返回;}

现在是我认为我搞砸的部分.我从这个指南(编译动态或共享对象库部分):

dierre@cox:~/Scrivania/provajni$ gcc -I"/usr/lib/jvm/java-6-sun/include" -I"/usr/lib/jvm/java-6-sun/include/linux" -o hellolib.so -shared -Wl,-soname,hello.so Hello.cpp -static -lc

生成文件hellolib.so

但是当我尝试使用 java Hello 运行它时,出现此错误:

线程main"中的异常java.lang.UnsatisfiedLinkError:java.library.path中没有hellolib在 java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)在 java.lang.Runtime.loadLibrary0(Runtime.java:823)在 java.lang.System.loadLibrary(System.java:1028)在你好.<clinit>(Hello.java:4)找不到主类:您好.程序将会退出.

我什至试过这个:

 LD_LIBRARY_PATH=`pwd`导出 LD_LIBRARY_PATH

没有结果.

我知道我在做一些非常愚蠢的事情,但我无法弄清楚它是什么.动态库是使用 -shared 选项生成的,不是吗?

更新 #1

我试过 static { System.load("/home/dierre/Scrivania/provajni/hellolib.so");} 看看这是否有效,但现在:

线程main"中的异常 java.lang.UnsatisfiedLinkError:/home/dierre/Scrivania/provajni/hello.so:/home/dierre/Scrivania/provajni/hello.so: 未定义符号: _ZSt4cout在 java.lang.ClassLoader$NativeLibrary.load(Native Method)在 java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)在 java.lang.ClassLoader.loadLibrary(ClassLoader.java:1699)在 java.lang.Runtime.load0(Runtime.java:770)在 java.lang.System.load(System.java:1003)在你好.<clinit>(Hello.java:4)

更新 #2好的,为了解决 Update #1 问题,我必须使用 g++ 插入 gcc,显然.但是仍然无法使用 load 方法.我似乎无法告诉它正确的路径.

解决方案

本地库可以由 loadLibrary 加载,并具有有效的名称.以libXXXX.so 为例,对于linux 家族,你的hellolib.so 应该重命名为libhello.so.顺便说一下,我用jni开发java,我会把实现和native interface(.c或者.cpp)分开.

静态{System.loadLibrary("你好");//将加载 libhello.so}

实现头文件(HelloImpl.h):

#ifndef _HELLO_IMPL_H#define _HELLO_IMPL_H#ifdef __cplusplus外部C"{#万一无效sayHello();#ifdef __cplusplus}#万一#万一

HelloImpl.cpp:

#include "HelloImpl.h"#include 使用命名空间标准;无效说你好(){cout<<你好世界!"<<结束;返回;}

Hello.c(我更喜欢用c编译jni):

#include #include "Hello.h"#include "HelloImpl.h"JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {问好();返回;}

最后,我们可以通过一些步骤来编译它们:

  1. 编译obj(生成HelloImpl.o)

<块引用>

g++ -c -I"/opt/java/include"-I"/opt/java/include/linux" HelloImpl.cpp

  1. 使用 .o 编译 jni

<块引用>

g++ -I"/opt/java/include"-I"/opt/java/include/linux" -o libhello.so -shared-Wl,-soname,hello.so Hello.c HelloImpl.o -static -lc

在第 2 步中,我们使用 g++ 来编译它.这是非常重要的.你可以看到 如何混合 C 和 C++

编译后可以用nm查看函数命名:

$ nm libhello.so |grep 说00000708 T Java_Hello_sayHello00000784 t _GLOBAL__I_sayHello00000718 T 说你好

有一个标记为 T 的 Java_Hello_sayHello.它应该与您的本地方法名称完全相同.如果一切正常.你可以运行它:

$ java -Djava.library.path=.你好你好世界!

I'm using Ubuntu 10.10

So that's what I did.

Hello.java:

class Hello {
        public native void sayHello();

        static { System.loadLibrary("hellolib"); }

        public static void main(String[] args){
                Hello h = new Hello();
                h.sayHello();
        }
}

Then I ran the follwing commands:

dierre@cox:~/Scrivania/provajni$ javac Hello.java

dierre@cox:~/Scrivania/provajni$ javah -jni Hello 

I've obtained Hello.class and Hello.h.

Hello.h:

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

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

#ifdef __cplusplus
}
#endif
#endif

Then I created Hello.cpp:

#include <jni.h>
#include "Hello.h"
#include  <iostream>

using namespace std;

JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {
        cout << "Hello World!" << endl;
        return;
}

And now the part where I think I screwed up. I was inspired by this guide (Compile the Dynamic or Shared Object Library section):

dierre@cox:~/Scrivania/provajni$ gcc -I"/usr/lib/jvm/java-6-sun/include" -I"/usr/lib/jvm/java-6-sun/include/linux" -o hellolib.so -shared -Wl,-soname,hello.so Hello.cpp -static -lc

that generates the file hellolib.so

But when I try to run it with java Hello I have this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no hellolib in java.library.path
 at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
 at java.lang.Runtime.loadLibrary0(Runtime.java:823)
 at java.lang.System.loadLibrary(System.java:1028)
 at Hello.<clinit>(Hello.java:4)
Could not find the main class: Hello.  Program will exit.

I even tried this:

  LD_LIBRARY_PATH=`pwd`
  export LD_LIBRARY_PATH

with no results.

I know I'm doing something extremely stupid but I can't figure out what it is. The dynamic lib is generated with the -shared option, isn't it?

Update #1

I tried static { System.load("/home/dierre/Scrivania/provajni/hellolib.so"); } to see if that worked but now:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/dierre/Scrivania/provajni/hello.so: /home/dierre/Scrivania/provajni/hello.so: undefined symbol: _ZSt4cout
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1699)
    at java.lang.Runtime.load0(Runtime.java:770)
    at java.lang.System.load(System.java:1003)
    at Hello.<clinit>(Hello.java:4)

Update #2 Ok, to solve the Update #1 problem I had to use g++ insted of gcc, obviously. Still having trouble to use the load method though. I can't seem to tell it the right path.

解决方案

Native library can be loaded by loadLibrary with a valid name. By example, libXXXX.so for linux family, your hellolib.so should rename to libhello.so. By the way, I develop java with jni, I will separate the implementation and native interface (.c or .cpp).

static {
    System.loadLibrary("hello"); // will load libhello.so
}

The implementation header(HelloImpl.h):

#ifndef _HELLO_IMPL_H
#define _HELLO_IMPL_H

#ifdef __cplusplus
        extern "C" {
#endif

        void sayHello ();

#ifdef __cplusplus
        }
#endif

#endif

HelloImpl.cpp:

#include "HelloImpl.h"
#include  <iostream>

using namespace std;

void sayHello () {
    cout << "Hello World!" << endl;
    return;
}

Hello.c (I prefer to compile jni in c):

#include <jni.h>
#include "Hello.h"
#include "HelloImpl.h"

JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {
    sayHello();
    return;
}

Finally, we can compile them in some steps:

  1. compile obj (generate HelloImpl.o)

g++ -c -I"/opt/java/include" -I"/opt/java/include/linux" HelloImpl.cpp

  1. compile jni with .o

g++ -I"/opt/java/include" -I"/opt/java/include/linux" -o libhello.so -shared -Wl,-soname,hello.so Hello.c HelloImpl.o -static -lc

in step 2, we use g++ to compile it. This is very important. yor can see How to mix C and C++

After compilation, you can check the function naming with nm:

$ nm libhello.so |grep say
00000708 T Java_Hello_sayHello
00000784 t _GLOBAL__I_sayHello
00000718 T sayHello

There is a Java_Hello_sayHello marked T. It should extactly equal to your native method name. If everything is ok. you can run it:

$ java -Djava.library.path=. Hello
Hello World!

这篇关于如何在 linux 上为 JNI 应用程序编译动态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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