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

后端 未结 3 444
后悔当初
后悔当初 2020-11-28 20:37

I\'m using Ubuntu 10.10

So that\'s what I did.

Hello.java:

class Hello {
        public native void sayHello();

           


        
3条回答
  •  我在风中等你
    2020-11-28 21:04

    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

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

    #include 
    #include "Hello.h"
    #include 
    
    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
    

提交回复
热议问题