我正在使用 jdk 1.8 和 Jython 2.7.0.jar 来执行我的 python 代码。但似乎 Jython 不支持 Python 的 2.7 功能“reportlab”模块。
我的 Java 代码:
package test;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test{
public Test()
{
System.out.println("Done!");
engine.eval("import sys");
engine.eval("print sys");
engine.eval("import reportlab");
engine.eval("print reportlab");
System.out.println("Done Here");
}
catch (ScriptException ex)
{
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex);
}
}
public static void main(String args[])
{
Test t=new Test();
}
}
我得到的错误:
run:
Done!
<module 'sys' (built-in)>
javax.script.ScriptException: ImportError: No module named reportlab in <script> at line number 1
Dec 28, 2015 8:57:52 PM test.Test <init>
SEVERE: null
javax.script.ScriptException: ImportError: No module named reportlab in <script> at line number 1
at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:202)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:42)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at test.Test.<init>(Test.java:28)
at test.Test.main(Test.java:40)
Caused by: Traceback (most recent call last):
File "<script>", line 1, in <module>
ImportError: No module named reportlab
at org.python.core.Py.ImportError(Py.java:328)
at org.python.core.imp.import_first(imp.java:877)
at org.python.core.imp.import_module_level(imp.java:972)
at org.python.core.imp.importName(imp.java:1062)
at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
at org.python.core.PyObject.__call__(PyObject.java:431)
at org.python.core.__builtin__.__import__(__builtin__.java:1232)
at org.python.core.imp.importOne(imp.java:1081)
at org.python.pycode._pyx2.f$0(<script>:1)
at org.python.pycode._pyx2.call_function(<script>)
at org.python.core.PyTableCode.call(PyTableCode.java:167)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1386)
at org.python.core.__builtin__.eval(__builtin__.java:497)
at org.python.core.__builtin__.eval(__builtin__.java:501)
at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:40)
... 4 more
构建成功(总时间:2 秒)
如果有人可以建议我一种远程执行 Python 脚本的替代方法,我将不胜感激。
此外,我在 python 中有一个大型代码库,在预定时间内用 java 重写是不可行的。