JAVA调用jython执行python文件
2017-03-21 22:18
393 查看
在web开发时候,经常在web环境使用本地环境的第三方库什么的,本文讲解java如何执行python文件。
网上说方法有三种,其实也就两种,下面着中介绍第二种通过(jython)。
小计一下:
1、Runtime.getRuntime()可以取得当前JVM的运行时环境,这也是在Java中唯一一个得到运行时环境的方法。
2、Runtime上其他大部分的方法都是实例方法,也就是说每次进行运行时调用时都要用到getRuntime方法。
3、Runtime中的exit方法是退出当前JVM的方法,估计也是唯一的一个吧,因为我看到System类中的exit实际上也是通过调用Runtime.exit()来退出JVM的,这里说明一下Java对Runtime返回值的一般规则(后边也提到了),0代表正常退出,非0代表异常中止,这只是Java的规则,在各个操作系统中总会发生一些小的混淆。
第二步:新建一个Test.java测试类
第三步:运行Test.java
提醒可能报如下异常:
原因:没有初始化 python.import.site
解决:
ok 完美
参考文章:
http://blog.csdn.net/fei33423/article/details/53491414
http://blog.csdn.net/guo_rui22/article/details/3765378
http://www.cnblogs.com/liinux/p/5481849.html
http://www.oschina.net/code/snippet_119671_5971
网上说方法有三种,其实也就两种,下面着中介绍第二种通过(jython)。
方法一
java.lang.Runtime
Runtime rt = Runtime.getRuntime(); try { Process proc = rt.exec("python /tmp/test.py"); }catch (Exception e){ e.printStackTrace(); }
小计一下:
1、Runtime.getRuntime()可以取得当前JVM的运行时环境,这也是在Java中唯一一个得到运行时环境的方法。
2、Runtime上其他大部分的方法都是实例方法,也就是说每次进行运行时调用时都要用到getRuntime方法。
3、Runtime中的exit方法是退出当前JVM的方法,估计也是唯一的一个吧,因为我看到System类中的exit实际上也是通过调用Runtime.exit()来退出JVM的,这里说明一下Java对Runtime返回值的一般规则(后边也提到了),0代表正常退出,非0代表异常中止,这只是Java的规则,在各个操作系统中总会发生一些小的混淆。
第二种(重点)
调用jython API
第一步:添加依赖<!-- https://mvnrepository.com/artifact/org.python/jython --> <dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.0</version> </dependency>
第二步:新建一个Test.java测试类
import org.python.util.PythonInterpreter; import java.util.Properties; /** * Author: 遇见小星 * Email: tengxing7452@163.com * Date: 17-3-21 * Time: 下午8:18 * Describe: jpython test */ public class Test { public static void main(String []args){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('Mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); interpreter.execfile("/tmp/test.py"); interpreter.exec("print 'created by tengxing on 2017.3'"); } }
第三步:运行Test.java
Testing started at 下午9:40 ... Tue this is test.py created by tengxing on 2017.3! 进程已结束,退出代码0
提醒可能报如下异常:
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site Determine if the following attributes are correct:
原因:没有初始化 python.import.site
解决:
public class Test { public static void main(String []args){ Properties props = new Properties(); props.put("python.home", "path to the Lib folder"); 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(preprops, props, new String[0]); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); interpreter.execfile("/tmp/test.py"); interpreter.exec("print 'created by tengxing on 2017.3!'"); } }
ok 完美
//调用python中的方法,并且打印结果 PyFunction func = (PyFunction) interpreter.get("adder",PyFunction.class); int a = 2010, b = 2; PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b)); System.out.println("anwser = " + pyobj.toString());
参考文章:
http://blog.csdn.net/fei33423/article/details/53491414
http://blog.csdn.net/guo_rui22/article/details/3765378
http://www.cnblogs.com/liinux/p/5481849.html
http://www.oschina.net/code/snippet_119671_5971
相关文章推荐
- Java实现调用jython执行python文件的方法
- Jython:java调用python文件之第三方包路径问题
- Jython:java调用python文件之第三方包路径问题
- Java如何调用可执行文件和批处理命令
- Java调用批处理或可执行文件
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值(如果有的话)
- java调用Python文件
- Python_Java调用Jython
- Java调用批处理或可执行文件
- java Runtime Process调用可执行文件,子进程阻塞
- java中调用其他文件(可执行程序,word,浏览器,bat……等)
- Jython_Java 调用python
- Java调用可执行文件和批处理命令
- Java调用批处理或可执行文件
- C/C++调用JVM -可执行Java文件方案
- Java如何调用可执行文件和批处理命令
- 用java程序调用ffmpeg执行视频文件格式转换flv
- 用java程序调用ffmpeg执行视频文件格式转换flv
- 从 stdin 读取输入的C程序示例 && python脚本调用可执行文件
- JAVA调用系统命令或可执行文件