在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 ...
随机推荐
- Clausen Functions (and related series, functions, integrals)
Since the Clausen functions are intimately related to a number of other important special functions, ...
- robotframe常用的类库、对应的方法和属性
robotframe常用的类库.对应的方法和属性
- gitlab的搭建与汉化
gitlab的搭建:内存最好2G以上 yum -y install curl unzip policycoreutils git wget 安装相关依赖包 所有gitlab rpm包的 ...
- DL4J之CNN对今日头条文本分类
一.数据集介绍 数据来源:今日头条客户端 数据格式如下: 6551700932705387022_!_101_!_news_culture_!_京城最值得你来场文化之旅的博物馆_!_保利集团,马未都, ...
- PyQt5剪切板操作
1.使用剪切板import sys,mathfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import ...
- Golang核心编程
源码地址: https://github.com/mikeygithub/GoCode 第1章 1Golang 的学习方向 Go 语言,我们可以简单的写成 Golang 1.2Golang 的应用领域 ...
- Executor、Executors、ExecutorService多线程操作
Executor:一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable command),该方法接收一个Runable实例,它用来执行一 ...
- 【原】python总结
python3浅拷贝和深拷贝:https://www.jianshu.com/p/c7e72fcad407
- tf.app.run()的作用
tf.app.run() 如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test) 如果你的代码中的入口函数叫main(), ...
- 在自定义的widget中引入图表后,运行时出现TypeError #1009错误
本人网上查找了很多资料,其中大部分都是关于Flash中的动画效果问题,与这里的问题关系型不太大.故把问题的解决方法写在这里,与人方便,与己方便. 方法一: 1.在自定义的widget中添加如下两个方法 ...