且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 23:41:16

native库可以由loadLibrary加载有效的名称。例如,lib * XXXX *。因此对于linux系列,你的hellolib.so应该重命名为libhello.so。
顺便说一下,我用jni开发java,我将分离实现和本地界面(.c或.cpp)。

  static {
System.loadLibrary(hello); //将加载libhello.so
}

实现头(HelloImpl.h):

  #ifndef _HELLO_IMPL_H 
#define _HELLO_IMPL_H

#ifdef __cplusplus
extern C{
#endif

void sayHello();

#ifdef __cplusplus
}
#endif

#endif

HelloImpl.cpp:

  #includeHelloImpl.h
#include< iostream>

using namespace std;

void sayHello(){
cout<< 你好,世界! << endl;
return;
}

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

  #include< jni.h> 
#includeHello.h
#includeHelloImpl.h

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

最后,我们可以通过以下步骤编译它们:


  1. compile obj(generate HelloImpl.o)




g ++ -c -I/ opt / java / include
-I/ opt / java / include / linuxHelloImpl.cpp





  1. 使用.o编译jni




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


在步骤2中,我们使用g ++来编译它。这个非常重要。您可以查看如何混合C和C ++



编译之后,可以使用nm检查函数命名:

  $ nm libhello。所以| grep说
00000708 T Java_Hello_sayHello
00000784 t _GLOBAL__I_sayHello
00000718 T sayHello


$ b b

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

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


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, lib*XXXX*.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!