我正在尝试在Jython中创建和转换对象,我收到以下错误:
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经验的人可以告诉我我做错了什么,那将非常感激。
答案 0 :(得分:0)
这是一个非常晚的答案,但对于可能面临同样问题的其他人:我只是遇到了我认为是同样的错误,并修复了它。我猜测你的Python类的声明不会从你的界面继承。
例如,ixnet.py:
import IxNetType
class IxNet(IxNetType):
...
这就是你应该拥有的。相反,您可能只是将IxNet声明为:
class IxNet:
...
将产生错误:&#34; java.lang.ClassCastException:org.python.core.PySingleton无法强制转换为resources.ixia.IxNetType&#34;