2015-01-12 34 views
2

我對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"); 

    } 
} 

請讓我知道我要去錯了,或者如果我在想念着一些錯誤。

+0

基本上,trail.py是一個腳本進行對話的硬件,並返回一些信息。由於保密問題,我無法披露此信息。我無權修改腳本。該腳本使用了很多Ctypes功能和其他本地庫。我的Java應用程序需要運行這個腳本並使用輸出。最近我做了很多搜索,但找不到合適的方法。目前,我最終使用Runtime.getRuntime()。exec()來運行腳本,但這似乎並不是一個理想的方法。任何人都可以在這種情況下用最好的方法幫助我嗎? – user817737

+0

@mzjn當我用Jython從Java運行下面的Python腳本我得到這個錯誤:進口ctypes的 P = ctypes.create_string_buffer(「你好」) 打印p.value 請讓我知道如果你需要更多的信息。我不能用任何方法解決這個問題。 – user817737

+0

建議:編輯問題,而不是在評論中添加信息。 – mzjn

回答

0

CPython(2.7)中的ctypes模塊定義了一個create_string_buffer函數。 Jython中的ctypes模塊(2.7 beta 3)沒有。

我真的不知道比這更多,但似乎有理由得出結論,在Jython中對ctypes的支持是不完整的。另請參見:

+1

好吧,那麼從java執行這樣的python腳本的最佳方式是什麼? – user817737

+0

除了已經使用的'Runtime.getRuntime()。exec()'之外,我想不出任何東西。 – mzjn

+0

Java腳本API可能是一個選項。請參閱http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html。 – mzjn