Java通过JYTHON调用python
Java调用python代码,目前只支持到python2.7
maven pom.xml
maven pom.xml
<!-- https://mvnrepository.com/artifact/org.python/jython -->
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.1b3</version>
</dependency>Java代码:
import java.util.Properties;
import org.python.util.PythonInterpreter;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Python环境初始化
Properties props = new Properties();
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(props, preprops, new String[] {});
// 实例化环境和代码执行
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=("mod","hello world!!!","Wed","Thu","Fri","Sat","Sun");");
interpreter.exec("print days[1]");
}
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: Unity3D之如何将包大小减少到极致
- 下一篇: java调用python方法总结
