在java中调用python方法
1、http://sourceforge.net/projects/jython/下载jython包,把其中的jython.jar添加到工程目录
示例:
1、摘自:http://blog.csdn.net/anbo724/article/details/6608632
1.在java类中直接执行python语句
- import javax.script.*;
- import org.python.util.PythonInterpreter;
- import java.io.*;
- import static java.lang.System.*;
- public class FirstJavaScript
- {
- 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];");
- }//main
- }
这样得到的结果是Tue,在控制台显示出来,这是直接进行调用的。
2.在java中调用本机python脚本中的函数:
首先建立一个python脚本,名字为:my_utils.py
- def adder(a, b):
- return a + b
然后建立一个java类,用来测试,
java类代码 FirstJavaScript:
- import javax.script.*;
- import org.python.core.PyFunction;
- import org.python.core.PyInteger;
- import org.python.core.PyObject;
- import org.python.util.PythonInterpreter;
- import java.io.*;
- import static java.lang.System.*;
- public class FirstJavaScript
- {
- public static void main(String args[])
- {
- PythonInterpreter interpreter = new PythonInterpreter();
- interpreter.execfile("C:\\Python27\\programs\\my_utils.py");
- 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());
- }//main
- }
得到的结果是:anwser = 2012
3.使用java直接执行python脚本,
建立脚本inputpy
- #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)
建立java类,调用这个脚本:
- import javax.script.*;
- import org.python.core.PyFunction;
- import org.python.core.PyInteger;
- import org.python.core.PyObject;
- import org.python.util.PythonInterpreter;
- import java.io.*;
- import static java.lang.System.*;
- public class FirstJavaScript
- {
- public static void main(String args[])
- {
- PythonInterpreter interpreter = new PythonInterpreter();
- interpreter.execfile("C:\\Python27\\programs\\input.py");
- }//main
- }
得到的结果是:
- hello
- [3, 5, 2, 0, 6]
- [0, 2, 3, 5, 6]
- [0, 2, 3, 5, 6, 0]
- 2
- 3
在java中调用python方法的更多相关文章
- 如何在Java中调用Python代码
有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...
- 在Java中调用Python
写在前面 在微服务架构大行其道的今天,对于将程序进行嵌套调用的做法其实并不可取,甚至显得有些愚蠢.当然,之所以要面对这个问题,或许是因为一些历史原因,或者仅仅是为了简单.恰好我在项目中就遇到了这个问题 ...
- 在Java中调用Python代码
极少数时候,我们会碰到类似这样的问题:与A同学合作写代码, A同学只会写Python,不熟悉Java ,而你只会写Java不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方 ...
- C#中调用python方法
最近因为项目设计,有部分使用Python脚本,因此代码中需要调用python方法. 1.首先,在c#中调用python必须安装IronPython,在 http://ironpython.codepl ...
- python爬虫简单实现,并在java中调用python脚本,将数据保存在json文件中
# coding:utf-8 import urllib2 from bs4 import BeautifulSoup import json import sys reload(sys) sys.s ...
- 使用Runtime.getRuntime().exec()在java中调用python脚本
举例有一个Python脚本叫test.py,现在想要在Java里调用这个脚本.假定这个test.py里面使用了拓展的包,使得pythoninterpreter之类内嵌的编译器无法使用,那么只能采用ja ...
- [转]如何在Java中调用DLL方法
转载地址:http://developer.51cto.com/art/200906/129773.htm Java语言本身具有跨平台性,如果通过Java调用DLL的技术方便易用,使用Java开发前台 ...
- odoo14 button 事件调用python方法如何传递参数
1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...
- python基础----继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法
一.什么是继承 继承是一种创建新的类的方式,在pyth ...
随机推荐
- 7、源与值(Source/Values)
学习目录:树莓派学习之路-GPIO Zero 官网地址:https://gpiozero.readthedocs.io/en/stable/source_values.html 环境:UbuntuMe ...
- netty(五) channel
问题 channel 是如何处理发送一半中断后继续重发的 channel 具体作用是什么 概述 这一节我们将介绍 Channel 和内部接口 Unsafe .其中Unsafe 是内部接口,聚合在Cha ...
- 支付接口API
//微信支付SDK https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
- 【CSS选择器】
" 目录 一.介绍 二.语法 三.引入方式 1. 行内样式 2. 嵌入式 3. 外部样式 四.选择器 1. 基本选择器 2. 组合选择器 3. 属性选择器 4. 不常用选择器 5. 分组和嵌 ...
- SpringCloud全家桶学习之路由网关----Zuul(六)
一.Zuul概述 (1)Zuul是什么? Zuul包含了对请求的路由和过滤的两个最主要的功能,其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础:而过滤功能则负责对请求的 ...
- SQL 游标介绍及使用
游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获取记录,并赋给主变量,交由主语言进一步处理. 游标是处理 ...
- redis的使用1
学Linux已经将近一个月了,Linux中讲到的redis的使用,到现在还不回具体的使用在php中,今天周末,于是想把redis的使用搞懂. 网上的资料不算多,但还需要硬着头皮学.其中找到这样一篇关于 ...
- tkinter的listbox、radiobutton和checkbutton学习(2)
1.tkinter的listbox 1.1 代码 #第1步,导出模块 import tkinter as tk #定义窗口,及其标题.大小和位置 win = tk.Tk() win.title('Li ...
- 需要写的CSS博客
重绘与回流 BFC 水平垂直居中 定位 基线各种线 inline-block,img标签空字符
- iOS 混合开发之 Cordova 实践
在15年时,之前公司使用 Cordova 做混合开发使用,后来公司没有用到了,现在重新记录下. Cordova (官网:http://cordova.apache.org/)简介: Apache Co ...