简体   繁体   中英

Java use JNI to import shared library in C that uses 3rd party functionality (Python.h)

I have a problem with my JNI integration of "lib.so" that is compiled from "lib.c" that looks like:

#include <jni.h>
#include "messageService.h"
#include <Python.h>

PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;

JNIEXPORT jboolean JNICALL Java_Test_test
 (JNIEnv * pEnv, jclass clazz)
{
    Py_Initialize();
    pName = PyString_FromString("mylib");
    pModule = PyImport_Import(pName);
    Py_DECREF(pName);
    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule,"test");
        if (pFunc && PyCallable_Check(pFunc)) {
            PyObject_CallObject(pFunc, NULL);
            Py_DECREF(pFunc);
            if (PyErr_Occurred()){
                PyErr_Print();
                jclass exc = (*pEnv)->FindClass( pEnv, "java/lang/Exception" );
                if ( exc != NULL )
                    (*pEnv)->ThrowNew( pEnv, exc, "in C code; Error while executing \"test\"" );
                return (jboolean) 0;
            }
            return (jboolean) 1;
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            jclass exc = (*pEnv)->FindClass( pEnv, "java/lang/Exception" );
            if ( exc != NULL )
                (*pEnv)->ThrowNew( pEnv, exc, "in C code; Cannot find function \"test\"" );
            return (jboolean) 0;
        }
        Py_XDECREF(pFunc);
        return (jboolean) 0;
    }
    else {
        PyErr_Print();
        jclass exc = (*pEnv)->FindClass( pEnv, "java/lang/Exception" );
        if ( exc != NULL )
            (*pEnv)->ThrowNew( pEnv, exc, "in C code; Failed to load \"mylib\"" );
    }
    return (jboolean) 0;
}

and the header file generated with javah -jni ...

Creating the lib.so file works fine. My java programm:

public class Test {
    static{
        System.load("/pathtomyso/lib.so")
    }
    public static native boolean test();
    public static void main(String[] args){
        boolean result = false;
        try{
            result = Test.test();
            System.out.println(result);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}

The result when I run my programm: Exception in thread "main" java.lang.UnsatisfiedLinkError: /pathtomyso/lib.so: /pathtomyso/lib.so: undefined symbol: Py_Initialize

Do you know why the functionality Py_Initialize from Python.h is unknown?

EDIT:1 I googled and got a wrong answer it seems. Now I use:

cc lib.c -o lib.so  -I/usr/lib/jvm/java-7-openjdk-i386/include/ -I /usr/include/python2.7 -lpython2.7 -shared -lc

But still I have some errors. My python module uses python wrappers for blueZ Bluetooth-Stack. But somehow it cannot import the shared library integrated in this module:

File "/usr/local/lib/python2.7/dist-packages/mylib.egg/mylib/test.py", line 10, in <module>
    import bluetooth
  File "/usr/local/lib/python2.7/dist-packages/bluetooth/__init__.py", line 34, in <module>
    from bluez import *
  File "/usr/local/lib/python2.7/dist-packages/bluetooth/bluez.py", line 6, in <module>
    import _bluetooth as _bt
/usr/local/lib/python2.7/dist-packages/bluetooth/_bluetooth.so: undefined symbol: PyExc_ValueError

I met the similar problem and had solved it. My problem and solution: Java call liba.so via JNA, liba.so call libb.so, libb.so call python script. In the liba.so, you should use RTLD_NOW | RTLD_GLOBAL in the dlopen. That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM