如何在 Linux 上为 JNI 应用程序编译动态库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3950635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:43:54  来源:igfitidea点击:

How to compile dynamic library for a JNI application on linux?

javac++linuxjava-native-interfacecompilation

提问by dierre

I'm using Ubuntu 10.10

我正在使用Ubuntu 10.10

So that's what I did.

所以这就是我所做的。

Hello.java:

你好.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.classand Hello.h.

我已经获得Hello.classHello.h

Hello.h:

你好.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:

然后我创建了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 inspiredby this guide (Compile the Dynamic or Shared Object Librarysection):

现在是我认为我搞砸的部分。我受到指南的启发编译动态或共享对象库部分)

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

生成文件 hellolib.so

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

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

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?

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

Update #1

更新 #1

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

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

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 #2Ok, to solve the Update #1problem I had to use g++insted of gcc, obviously. Still having trouble to use the loadmethod though. I can't seem to tell it the right path.

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

采纳答案by qrtt1

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

本机库可以由 loadLibrary 以有效名称加载。例如,对于 linux 系列,lib XXXX.so,您的 hellolib.so 应重命名为 libhello.so。顺便说一句,我用jni开发java,我会把实现和native interface(.c或.cpp)分开。

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

The implementation header(HelloImpl.h):

实现头文件(HelloImpl.h):

#ifndef _HELLO_IMPL_H
#define _HELLO_IMPL_H

#ifdef __cplusplus
        extern "C" {
#endif

        void sayHello ();

#ifdef __cplusplus
        }
#endif

#endif

HelloImpl.cpp:

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):

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

#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)
  1. 编译 obj(生成 HelloImpl.o)

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

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

  1. compile jni with .o
  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

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

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

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

编译完成后,可以用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:

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

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

回答by Gordon Thompson

This complains about the C++ symbols not being available. I seem to remember, when I use to do JNI stuff all of the time that there were problems linking in C++ libraries and we always stuck to plain old C

这抱怨 C++ 符号不可用。我似乎记得,当我一直在做 JNI 的事情时,在 C++ 库中存在链接问题,我们总是坚持使用普通的旧 C

If you change your code so that it's standard C (and rename the file):

如果您更改代码使其成为标准 C(并重命名文件):

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

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

And compile it

并编译它

gcc -I/usr/lib/jvm/java-6-openjdk/include  -o libhellolib.so -shared Hello.c

It works

有用

java -Djava.library.path=`pwd` Hello
Hello World

回答by Sandipan

Finally my code works. This is hello.java

最后我的代码有效。这是你好.java

public class hello {
  public native void sayHello(int length) ;
  public static void main (String args[]) {
    String str = "I am a good boy" ;
    hello h = new hello () ;
    h.sayHello (str.length() ) ;
  }
  static {
    System.loadLibrary ( "hello" ) ;
  }
}

You should compile it as :

您应该将其编译为:

$ javac hello.java 

To create .h file you should run this command:

要创建 .h 文件,您应该运行以下命令:

$ javah -jni hello

This is hello.h:

这是hello.h

JNIEXPORT void JNICALL Java_hello_sayHello
(JNIEnv *, jobject, jint);

Here is hello.c:

这是hello.c

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

JNIEXPORT void JNICALL Java_hello_sayHello
  (JNIEnv *env, jobject object, jint len) {
  printf ( "\nLength is %d", len ); }

To compile this and to create a shared library we have to run this command :

要编译它并创建共享库,我们必须运行以下命令:

$ gcc -I/usr/lib/jvm/java-6-openjdk/include -o libhello.so -shared hello.c

Then finally run this one :

然后最后运行这个:

$ java -Djava.library.path=. hello