JAVA调用Python及Shell
来源:互联网 发布:范雎怎么读sui 知乎 编辑:程序博客网 时间:2024/05/22 02:17
这是最近项目中有涉及到的一个点,网上有很多这样的方法,我只是想作为自己的工作日记保存下来,也当做一个总结。
其中,这涉及到一个类叫process,其实大家一看代码就知道了。
//String cmd = "sh /home/test/test.sh“ ;
String cmd = "python /home/test/test.py“ ;//windows下一样 只是要注意路径的格式,调用bat其实也类似
Process process = Runtime.getRuntime().exec(cmd);
python或shell脚本有返回值的,可读取process.getInputStream()流,IO流的读写就不详细说了。
另外,也可用Jython来更加灵活的调用python脚本或代码,具体做法先下载jar包导入,主要用到其中的PythonInterpreter类。
Exp:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter有很多重载的执行方法,可自行查询API选用。
若想调用python脚本中的某些方法,可单独获取,参照反射机制
PyFunction func = (PyFunction)interpreter.get("testFun",PyFunction.class);
testFun为python中的方法名,若方法需要传参,可通过下面方法传参执行
String a = "hello";
String b = "world";
PyObject pyobj = func.__call__(new PyString(a), new PyString(b));
end。
- JAVA调用Python及Shell
- java 调用python,shell脚本
- python调用shell
- Python 调用shell脚本
- python调用shell
- python调用shell命令
- python中调用shell
- python调用Shell脚本
- Python调用shell命令
- python调用Shell脚本
- python调用shell脚本
- python调用shell命令
- python调用shell
- python调用shell命令
- python调用shell总结
- python调用shell命令
- shell调用python
- python调用shell脚本
- hdu 1402 A * B Problem Plus || poj 2389 Bull Math
- 设计模式 Concurrency 之 Half_Sync_Half_Async 半同步半异步模式
- 关于是否为Ajax请求的判断
- 彩色网格
- Qt自定义菜单项
- JAVA调用Python及Shell
- mybatis集成spring操作数据库(以注解的方式)
- RocketMQ——客户端篇:Producer/Consumer的实例对象
- win10系统输入法图标不见了怎么找回
- Java:从字符串中提取字符的一系列方法及代码示例
- web前端网站合集(自己收集)
- Ubuntu16.04 配置虚拟主机
- MySQL学习笔记——20170808
- index的注释与理解(1)JS遮罩层如何写之Window.onload