[英]How can I create a simple JNI library that I can simply call methods from
[英]Can I call Python library using JNI?
我知道有jython解決方案,但jython無法加載wtxl和rdxl; 它只能加載有限的pip庫。 所以我想寫JNI去做。
主要程序如下:Java < - > JNI < - > C / C ++ Python接口< - >本地Python環境< - >我的html2excel python庫
問題是它無法導入html2excel庫。 我使用C / C ++代碼:
int to_excel(const std::string & htmlfile)
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('/home/allen/python')");
PyObject *html2excelModule = PyImport_ImportModule("html2excel");
if (!html2excelModule )
{
std::cerr << "Error: open python html2excel failed" << std::endl;
Py_Finalize();
return -1;
}
...
}
上面的程序告訴我這個錯誤
ImportError:沒有名為html2excel的模塊
html2excel.py位於/ home / allen / python中。 從shell運行時沒關系。
$python
>>>import sys
>>>sys.path.append('/home/allen/python')
>>>import html2excel
為什么我的JNI庫無法導入現有的html2excel模塊? 先感謝您!
錯誤消息顯示rlm_python:EXCEPT :: /usr/lib/python2.7/lib-dynload/_io.so:未定義的符號:_Py_ZeroStruct
應該在JNI實現中使用dlopen手動加載libpython2.7.so。
詳細的解決方案是https://github.com/FreeRADIUS/freeradius-server/pull/415 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.