Jython导入驻留在不同目录中的“同名”Java文件

时间:2014-01-14 19:31:55

标签: python error-handling jython

我将使用Jython导入两个据称相同的java文件进行比较。比较是在返回值,签名和构造函数的基础上进行的(这很好,但问题在于导入)。一个文件位于submission文件夹中,另一个文件位于solution文件夹中。问题如下,

当我运行以下代码时,

    sys.path.append('Solution/')
    import GeometricShape
    sys.path.pop()

    sys.path.append('Submission/')
    import GeometricShape    <---- the imported module is the same as the one above
    sys.path.pop()

第二个导入的GeometricShape是solution文件夹中的一个,而它应该是submission文件夹。按del(sys.modules["GeometricShape"])删除模块仍会以某种方式将第一个GeometricShape保留在内存中。 imp.load_source不起作用,因为它不是python源文件。在目录中创建__init__并将模块导入from Solution import GeometricShapes不起作用,因为我收到以下错误,

java.lang.NoClassDefFoundError: Solution/GeometricShape (wrong name: GeometricShape)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at org.python.core.SyspathJavaLoader.findClass(SyspathJavaLoader.java:123)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.python.core.Py.loadAndInitClass(Py.java:909)
    at org.python.core.Py.findClassInternal(Py.java:844)
    at org.python.core.Py.findClassEx(Py.java:895)
    at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:133)
    at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:28)
    at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:122)
    at org.python.core.PyJavaPackage.__findattr_ex__(PyJavaPackage.java:137)
    at org.python.core.PyObject.__findattr__(PyObject.java:863)
    at org.python.core.packagecache.PackageManager.lookupName(PackageManager.java:136)
    at org.python.core.PyModule.impAttr(PyModule.java:106)
    at org.python.core.imp.import_next(imp.java:720)
    at org.python.core.imp.ensureFromList(imp.java:888)
    at org.python.core.imp.ensureFromList(imp.java:856)
    at org.python.core.imp.import_module_level(imp.java:850)
    at org.python.core.imp.importName(imp.java:917)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
    at org.python.core.PyObject.__call__(PyObject.java:357)
    at org.python.core.__builtin__.__import__(__builtin__.java:1173)
    at org.python.core.imp.importFromAs(imp.java:1011)
    at org.python.core.imp.importFrom(imp.java:987)
    at org.python.pycode._pyx2.get_solution$4(D:\Grader\autograder.py:48)
    at org.python.pycode._pyx2.call_function(D:\Grader\autograder.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:120)
    at org.python.core.PyFunction.__call__(PyFunction.java:307)
    at org.python.pycode._pyx2.main$9(D:\Grader\autograder.py:158)
    at org.python.pycode._pyx2.call_function(D:\Grader\autograder.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:120)
    at org.python.core.PyFunction.__call__(PyFunction.java:307)
    at org.python.pycode._pyx2.f$0(D:\Grader\autograder.py:159)
    at org.python.pycode._pyx2.call_function(D:\Grader\autograder.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1275)
    at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:235)
    at org.python.util.jython.run(jython.java:247)
    at org.python.util.jython.main(jython.java:129)

有解决方法吗?

...谢谢

0 个答案:

没有答案