不论在Windows上还是在Linux上,均需通过如下命令安装jython,只使用standalone而不安装是不行的:
1 |
java -jar jython-installer-2.7.1.jar --console |
然后按照提示进行安装,请注意最后jython所在的路径需要在代码中配置,否则会报各种诡异的错误(例如找不到random模块等):
在Linux环境下,py文件需要与jar包在同一目录下。可以使用如下的工具类,注意其中如何配置的jython安装目录和py文件所在目录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
package com.test.utils; import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.python.core.Py; import org.python.core.PyFunction; import org.python.core.PyObject; import org.python.core.PySystemState; import org.python.util.PythonInterpreter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.test.ConfigBean; import com.test.spring.SpringBeanFactory; public class PyUtils { private static Logger logger = LoggerFactory.getLogger(PyUtils.class); private static ConfigBean configBean = SpringBeanFactory.getBean("configBean"); private PythonInterpreter interpreter; private PyFunction encryptFun; private PyFunction decryptFun; private static PyUtils self; public String decrypt(String key, String data) { PyObject decryptFunctionResult = self.decryptFun.__call__(Py.newString(key), Py.newString(data)); return decryptFunctionResult.toString(); } public String encrypt(String key, String data) { PyObject result = self.encryptFun.__call__(Py.newString(key), Py.newString(data)); return result.toString(); } public static PyUtils getInstance() { if (self == null) { self = new PyUtils(configBean.getPythonDirectory());//此处是py脚本文件所在的目录 } return self; } private PyUtils(String path) { if (StringUtils.endsWithIgnoreCase("develop", configBean.getEnv())) { } else if (StringUtils.endsWithIgnoreCase("product", configBean.getEnv())) { Properties properties = new Properties(); properties.put("python.home", configBean.getJythonHome());//此处是jython安装后所在的目录,里面是有Lib目录等结构的 properties.put("python.console.encoding", "UTF-8"); properties.put("python.security.respectJavaAccessibility", "false"); properties.put("python.import.site", "false"); Properties systemProperties = System.getProperties(); PythonInterpreter.initialize(systemProperties, properties, new String[0]); } interpreter = new PythonInterpreter(); PySystemState pySystemState = Py.getSystemState(); pySystemState.path.add(path); interpreter.exec("import sys"); interpreter.exec("print sys.path"); logger.info("Python path={}", pySystemState.path.toString()); interpreter.execfile("1.py"); interpreter.execfile("2.py"); interpreter.execfile("3.py"); interpreter.execfile("4.py"); interpreter.execfile("5.py"); interpreter.execfile("6.py"); encryptFun = interpreter.get("encrypt", PyFunction.class); decryptFun = interpreter.get("decrypt", PyFunction.class); } public void close() { interpreter.cleanup(); interpreter.close(); } } |
参考资料:
1、https://www.jython.org/download
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » 在Windows和Linux下使用jython实现Java与Python互操作