在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 ...
随机推荐
- wsgiref模块
学习django框架之前,可以先学习一下wsgiref模块,熟悉前后端交互. 一.先介绍下wsgiref模块 WSGI(Web Server Gateway Interface)是一种规范,它定义了使 ...
- jQuery中$("input")与$(":input")的区别
$("input")表示获取页面所有的input元素 $(":input")选取表单中所有的input,select 和 button元素
- C语言:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。-使字符串的前导*号不得多于n个,若多余n个,则删除多余的*号,
//fun函数:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动. #include <stdio.h> void fun( char * ...
- 【嵌入式】ARM9复习
一.嵌入式系统基础 二.ARM处理器 1. 在每条指令后,用;//注释这条指令的寻址方式,以及实现的功能(25分) 注:变址寻址需要标注是基址加偏移.还是基址加索引,是前变址还是后变址.SUB SP, ...
- 150元搭建微型家庭服务器(支持DLAN,samda,aria2)
概览 看到有人用树莓派搭家庭服务器,感觉太不值了,300块都可以搭一台性能还可以的低功耗x86主机了,我搭一个100块顶多的服务器玩玩. 0.Linux服务器学习(比虚拟机双系统舒服多了) 1.流媒体 ...
- 基于SSH开发银行个人业务管理系统 附源码
开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 运行效果图
- 无刷新的批量图片上传插件.NET版
啥都不说,先上效果图: 这是一个网上的第三方组件,原版是php的,我用.NET重写了图片上传的处理,下面贴上代码 using System; using System.Collections.Gene ...
- 解决sublime不能安装packages的问题
问题如下:该问题产生的原因是因为默认的配置中无法访问 "https://packagecontrol.io/channel_v3.json"该文件造成的 解决: 1.下载 chan ...
- iOS 开发之 23种设计模式
整理了 iOS 开发中用到的设计模式: iOS 开发之 设计模式[一]原型模式 (Prototype pattern) iOS 开发之 设计模式[二]工厂方法模式 iOS 开发之 设计模式[三]抽象工 ...
- cocoapods diff: /../Podfile.lock: No such file or directory 解决方案
在运行之前的使用 CocoaPods 工程时,有时会报错:diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: ...