java调用python
来源:互联网 发布:whois域名查询系统 编辑:程序博客网 时间:2024/06/07 07:04
1.下载jpython.jar的链接:http://download.csdn.net/detail/haleyliu123/9862861
2.在Java类中直接执行Python语句
package com.lrq.test;import java.io.UnsupportedEncodingException;import java.util.Properties;import org.python.util.PythonInterpreter;public class FristPython { public static void main(String args[]) throws UnsupportedEncodingException { Properties props = new Properties(); props.put("python.console.encoding", "UTF-8"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interpreter = new PythonInterpreter(); String swords=new String("swords=('tianWen','渊虹','太阿','干将','莫邪','雪霁','水寒'); ".getBytes("ISO-8859-1"),"UTF-8"); interpreter.exec(swords); interpreter.exec("print swords[0].encode('utf-8');"); }}
结果:
#!/user/bin/pythondef plusFunc(a, b): return a + b
package com.lrq.test;import java.io.UnsupportedEncodingException;import org.python.core.PyFunction;import org.python.core.PyInteger;import org.python.core.PyObject;import org.python.util.PythonInterpreter;public class UsePythonFunction { public static void main(String args[]) throws UnsupportedEncodingException { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("test.py"); PyFunction func = (PyFunction) interpreter.get("plusFunc",PyFunction.class); int a =12, b = 2; PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b)); //转码 String newStr = new String(pyobj.toString().getBytes("iso8859-1"), "utf-8"); System.out.println("anwser = " + newStr); }}
结果:
4.使用java直接执行python脚本
#open files print 'hello' number=[3,5,2,0,6] print number number.sort() print number number.append(0) print number print number.count(0) print number.index(5)
package com.lrq.test;import org.python.util.PythonInterpreter;public class ExcutePythonFile{public static void main(String args[]){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("input.py"); }}结果:
阅读全文
0 0
- Java调用Python
- java调用Python文件
- java 调用python
- python调用java
- python 调用Java
- java中调用python
- Java调用Python
- Java调用Python脚本
- Java直接调用Python
- Java调用Python
- python调用java
- Java调用python
- java调用python
- java 调用python
- python调用java函数
- java调用Python
- java中调用python
- Java调用Python脚本
- 概率编程语言(Probabilistic Programming Languages)库 —— edward
- Dorado的一些技巧总结
- 八个移动应用的测试工具
- 梳理一下ios开发的环境、工具、框架
- Service xxx does not have a SELinux domain defined
- java调用python
- 《数据结构学习与实验指导》2-1:简单计算器
- JavaScript变量: 变量命名原则
- 冒泡排序
- hadoop-26
- 关于CodeBlocks安装后无法编译问题
- Tiling_easy version
- android基础学习11——结合ListView在界面上操作数据库
- 简单的HTML登录页面