我在我的web应用程序中使用嵌入式Jython (Version2.5.3),我尝试在我的Java代码中这样做:
PythonInterpreter pythonInterpreter = new PythonInterpreter();
pythonInterpreter.exec("import codecs");
pythonInterpreter.exec("codecs.getreader('utf8')");但我知道这个错误:
File "<string>", line 1, in <module>
File "__pyclasspath__/codecs$py.class", line 920, in getreader
LookupError: no codec search functions registered: can't find encoding 'utf8'如何正确获取“utf8”编码的读取器?
根据python文档,编码模块中应该有“utf8”编码:https://docs.python.org/2/library/codecs.html#standard-encodings (小写/大写和连字符/下划线都是允许的)。
我使用Windows 7,java 1.7.0_71。操作系统应该无所谓--这是在jboss上运行的web应用程序(7.2版)。这个问题发生在jython-独立的-2.5.3.jar和常规的jython-2.5.3.jar上。
发布于 2015-02-05 16:22:11
我设法找到了这个问题的解决方案--这是一个配置错误。在将代码更改为:
import java.util.Properties;
import org.python.util.PythonInterpreter;
(...)
Properties properties = new Properties();
properties.setProperty("python.path", pythonPath);
PythonInterpreter.initialize(System.getProperties(), properties, new String[] { "" });
PythonInterpreter pythonInterpreter = new PythonInterpreter();
pythonInterpreter.exec("import codecs");
pythonInterpreter.exec("codecs.getreader('utf8')");在部署的WAR中,pythonPath是jython-2.5.3.jar中“Lib”目录的路径。
最后,我用一些PostConstruct Spring方法包装了所有这些内容,这个方法在启动Spring时被调用。
https://stackoverflow.com/questions/28303395
复制相似问题