我的JNI集成了" lib.so"这是从" lib.c"编译而来的。看起来像是:
#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;
}
和使用javah -jni ...
生成的头文件创建lib.so文件可以正常工作。 我的java程序:
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());
}
}
}
运行程序时的结果: 线程&#34; main&#34;中的例外情况java.lang.UnsatisfiedLinkError:/pathtomyso/lib.so:/pathtomyso/lib.so:undefined symbol:Py_Initialize
你知道为什么Python.h中的Py_Initialize功能未知吗?
修改:1 我google了,似乎得到了一个错误的答案。 现在我用:
cc lib.c -o lib.so -I/usr/lib/jvm/java-7-openjdk-i386/include/ -I /usr/include/python2.7 -lpython2.7 -shared -lc
但我仍有一些错误。我的python模块使用python包装器为blueZ蓝牙堆栈。但不知何故,它无法导入集成在此模块中的共享库:
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
答案 0 :(得分:0)
我遇到了类似的问题并解决了它。我的问题和解决方案: Java通过JNA调用liba.so,liba.so调用libb.so,libb.so调用python脚本。在liba.so中,您应该使用RTLD_NOW | dlopen中的RTLD_GLOBAL。就是这样。