我對Java和python相當陌生。我有幾個python腳本,我需要從Java應用程序調用。目前我正在使用Jython 2.7 beta 3版本來執行此操作。我的python腳本使用Ctypes(create_string_buffer)中的方法。當我從Java運行此腳本時,會引發以下錯誤。Jython中的Ctypes支持
「模塊」對象有沒有屬性「create_string_buffer」
我的Java代碼如下:
import org.python.core.PyInstance;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import org.python.core.PyString;
public class Sample
{
PythonInterpreter interpreter = null;
public Sample()
{
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
this.interpreter = new PythonInterpreter();
}
void execfile(final String fileName)
{
this.interpreter.execfile(fileName);
}
void eval(final String className, final String opts)
{
this.interpreter.eval(className + "(" + opts + ")");
}
PyInstance createClass(final String className, final String opts)
{
return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}
public static void main(String gargs[])
{
Sample ie = new Sample();
ie.execfile("C:/Users/trail.py");
}
}
請讓我知道我要去錯了,或者如果我在想念着一些錯誤。
基本上,trail.py是一個腳本進行對話的硬件,並返回一些信息。由於保密問題,我無法披露此信息。我無權修改腳本。該腳本使用了很多Ctypes功能和其他本地庫。我的Java應用程序需要運行這個腳本並使用輸出。最近我做了很多搜索,但找不到合適的方法。目前,我最終使用Runtime.getRuntime()。exec()來運行腳本,但這似乎並不是一個理想的方法。任何人都可以在這種情況下用最好的方法幫助我嗎? – user817737
@mzjn當我用Jython從Java運行下面的Python腳本我得到這個錯誤:進口ctypes的 P = ctypes.create_string_buffer(「你好」) 打印p.value 請讓我知道如果你需要更多的信息。我不能用任何方法解決這個問題。 – user817737
建議:編輯問題,而不是在評論中添加信息。 – mzjn