2016-02-18 16 views
5

とクラスキャスト例外私が作成しようとJythonの中でオブジェクトをキャストし、私は次のエラーを受信して​​いています:ここでのJython - PyObject

Exception in thread "MainThread" java.lang.ClassCastException: org.python.core.PySingleton cannot be cast to resources.ixia.IxNetType 
at resources.ixia.IxNetFactory.create(IxNetFactory.java:34) 
at resources.ixia.IxiaTest.run(IxiaTest.java:34) 
at resources.ixia.IxiaTest.<init>(IxiaTest.java:14) 
at resources.ixia.IxiaTest.main(IxiaTest.java:42) 

はコードです:については

import org.python.core.PyObject; 
import org.python.util.PythonInterpreter; 

public class IxNetFactory { 

    private PyObject ixNetClass; 
    private PythonInterpreter interpreter; 

    public IxNetFactory(String script_dir) { 
     script_dir=script_dir.replace("\\", "/"); 

     interpreter = new PythonInterpreter(); 

     interpreter.exec("import sys");    
     interpreter.exec("sys.path.append('"+script_dir+"')"); 
     interpreter.exec("import time"); 
     interpreter.exec("import os"); 
     interpreter.exec("from ixnetworks import IxNet"); 
     //interpreter.exec("from utils import sm"); 
     //interpreter.exec("from utils import cpf"); 

     ixNetClass = interpreter.get("IxNet"); 
    } 

    /* 
    * Create an IxNet object 
    * 
    * Usage: ixNet.create(); 
    */ 
    public IxNetType create() { 
     PyObject ixNetObject = ixNetClass.__call__(); 
     return (IxNetType)ixNetObject.__tojava__(IxNetType.class); 
    } 

    public void close() { 
     interpreter.close(); 
    } 
} 

私の人生は私が間違っていることを理解することができません。私が読んだことのすべてから、私はこれを正しくやっているようですが、それを働かせることはできません。

のJythonの経験を持つ人は、私はそれがはるかに高く評価されるだろう間違ってやっているものを私に伝えることができれば。

+1

私は'IxNet'と' IxNetType'がどのように見えるのだろうか。これらのタイプのソースが閉じている場合は、露出を最小限に抑えながら問題を再現するような方法で書き直すことができますか? –

答えて

0

これは非常に遅い回答ですが、同じ問題に直面している可能性のある他の人々のために:私は同じエラーと思われるものを持っていて、修正しました。私はあなたのPythonクラスの宣言があなたのインターフェイスから継承していないと推測しています。例えば

、ixnet.py:

import IxNetType 

class IxNet(IxNetType): 
... 

これはあなたが持っているべきものです。

エラーが発生します
class IxNet: 
... 

:代わりに、あなたはおそらく同じようIxNetを宣言した "とjava.lang.ClassCastException:org.python.core.PySingletonはresources.ixia.IxNetTypeにキャストすることはできません"