java调用python
2017-03-20 20:45
337 查看
本文记录下使用jython包来实现java代码中调用Python。
一、Maven加入
二、代码
PythonDemo.java
test.py
三、参考文档
http://bugs.jython.org/issue2355
一、Maven加入
<dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.0</version> </dependency>
二、代码
PythonDemo.java
package com.bob.testjava.python; import org.python.util.PythonInterpreter; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.Properties; /** * Created by zhangmingbo on 3/20/17. */ public class PythonDemo { public static void main(String[] args) throws IOException { //Create interpreter Properties props = new Properties(); props.put("python.home", "path to the Lib folder"); props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses props.put("python.import.site", "false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interp = new PythonInterpreter(); String pythonCodeStr = new String(Files.readAllBytes(new File("test.py").toPath())); interp.exec(pythonCodeStr); } }
test.py
a = 2; b = 4; c = a * b; print "I am from file", c
三、参考文档
http://bugs.jython.org/issue2355
相关文章推荐
- [转]python调用Java-JPype使用介绍
- python调用Java-JPype使用介绍
- 使用thrift做c++,java和python的相互调用 - jinghong - ITeye技术网站
- java 调用python
- Java调用Python
- java调用python的webservice
- 在Java中调用Python
- 利用thrift在c++、java和python之间相互调用
- 基于XML-RPC的远程调用(Python,Java)
- JPype:实现在python中调用JAVA
- Python+Jpype 调用 Java 碰壁全记录
- Python_python调用Java-JPype使用介绍
- 基于XML-RPC的远程调用(Python,Java)
- java内调用perl、python、dll、cmd
- java调用Python文件
- 在Java中调用Python
- 利用thrift在c++,python,java之间相互调用
- 在Python中调用Java方法 Pyjnius
- python调用Java-JPype使用介绍(一)(转)
- java调用python的3种方式