1、http://sourceforge.net/projects/jython/下载jython包,把其中的jython.jar添加到工程目录

示例:

1、摘自:http://blog.csdn.net/anbo724/article/details/6608632

1.在java类中直接执行python语句

  1. import javax.script.*;
  2. import org.python.util.PythonInterpreter;
  3. import java.io.*;
  4. import static java.lang.System.*;
  5. public class FirstJavaScript
  6. {
  7. public static void main(String args[])
  8. {
  9. PythonInterpreter interpreter = new PythonInterpreter();
  10. interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
  11. interpreter.exec("print days[1];");
  12. }//main
  13. }

这样得到的结果是Tue,在控制台显示出来,这是直接进行调用的。

2.在java中调用本机python脚本中的函数:

首先建立一个python脚本,名字为:my_utils.py

  1. def adder(a, b):
  2. return a + b

然后建立一个java类,用来测试,

java类代码 FirstJavaScript:

  1. import javax.script.*;
  2. import org.python.core.PyFunction;
  3. import org.python.core.PyInteger;
  4. import org.python.core.PyObject;
  5. import org.python.util.PythonInterpreter;
  6. import java.io.*;
  7. import static java.lang.System.*;
  8. public class FirstJavaScript
  9. {
  10. public static void main(String args[])
  11. {
  12. PythonInterpreter interpreter = new PythonInterpreter();
  13. interpreter.execfile("C:\\Python27\\programs\\my_utils.py");
  14. PyFunction func = (PyFunction)interpreter.get("adder",PyFunction.class);
  15. int a = 2010, b = 2 ;
  16. PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
  17. System.out.println("anwser = " + pyobj.toString());
  18. }//main
  19. }

得到的结果是:anwser = 2012

3.使用java直接执行python脚本,

建立脚本inputpy

  1. #open files
  2. print 'hello'
  3. number=[3,5,2,0,6]
  4. print number
  5. number.sort()
  6. print number
  7. number.append(0)
  8. print number
  9. print number.count(0)
  10. print number.index(5)

建立java类,调用这个脚本:

  1. import javax.script.*;
  2. import org.python.core.PyFunction;
  3. import org.python.core.PyInteger;
  4. import org.python.core.PyObject;
  5. import org.python.util.PythonInterpreter;
  6. import java.io.*;
  7. import static java.lang.System.*;
  8. public class FirstJavaScript
  9. {
  10. public static void main(String args[])
  11. {
  12. PythonInterpreter interpreter = new PythonInterpreter();
  13. interpreter.execfile("C:\\Python27\\programs\\input.py");
  14. }//main
  15. }

得到的结果是:

  1. hello
  2. [3, 5, 2, 0, 6]
  3. [0, 2, 3, 5, 6]
  4. [0, 2, 3, 5, 6, 0]
  5. 2
  6. 3

分享: 

在java中调用python方法的更多相关文章

  1. 如何在Java中调用Python代码

    有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...

  2. 在Java中调用Python

    写在前面 在微服务架构大行其道的今天,对于将程序进行嵌套调用的做法其实并不可取,甚至显得有些愚蠢.当然,之所以要面对这个问题,或许是因为一些历史原因,或者仅仅是为了简单.恰好我在项目中就遇到了这个问题 ...

  3. 在Java中调用Python代码

    极少数时候,我们会碰到类似这样的问题:与A同学合作写代码, A同学只会写Python,不熟悉Java ,而你只会写Java不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方 ...

  4. C#中调用python方法

    最近因为项目设计,有部分使用Python脚本,因此代码中需要调用python方法. 1.首先,在c#中调用python必须安装IronPython,在 http://ironpython.codepl ...

  5. python爬虫简单实现,并在java中调用python脚本,将数据保存在json文件中

    # coding:utf-8 import urllib2 from bs4 import BeautifulSoup import json import sys reload(sys) sys.s ...

  6. 使用Runtime.getRuntime().exec()在java中调用python脚本

    举例有一个Python脚本叫test.py,现在想要在Java里调用这个脚本.假定这个test.py里面使用了拓展的包,使得pythoninterpreter之类内嵌的编译器无法使用,那么只能采用ja ...

  7. [转]如何在Java中调用DLL方法

    转载地址:http://developer.51cto.com/art/200906/129773.htm Java语言本身具有跨平台性,如果通过Java调用DLL的技术方便易用,使用Java开发前台 ...

  8. odoo14 button 事件调用python方法如何传递参数

    1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...

  9. python基础----继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法

    一.什么是继承                                                                          继承是一种创建新的类的方式,在pyth ...

随机推荐

  1. pandas库笔记

    本笔记为自学笔记 1.pandas.DataFrame() 一种保存矩阵的数据格式 grades_df = pd.DataFrame( data={'exam1': [43, 81, 78, 75, ...

  2. 基于Ubuntu和基于Debian的Linux Mint 20新特性一览

    导读 Linux Mint 20 将基于 Ubuntu 20.04 LTS,同时,其 LMDE(Linux Mint Debian Edition,Debian 版本)4 也将到来,LMDE 版本基于 ...

  3. Java数组和方法

    1. 数组可以作为方法的参数 package cn.itcast.day05.demo04; /* 数组可以作为方法的参数. 当调用方法的时候,向方法的小括号进行传参,传递进去的其实是数组的地址值. ...

  4. Javascript——(1)

    1.Javascript有两种解释表示形式:1)在html的<header>中写<script><script/>,另一种是将另一个文件保存为xxx.js文档,然后 ...

  5. python学习之rabbitmq

    0.讲述rabbit中各部分的含义及作用 https://www.jb51.net/article/75647.htm 1.rabbitMQ的安装 1)在安装rabbitmq之前需要先安装erlang ...

  6. Coursera-吴恩达机器学习课程笔记-Week4+5

    Neural networks non-linear hypotheses 非线性假设 Neural model:logistic unit 第一层 Input layer 最后一层 Outer la ...

  7. python opencv:代码执行时间计算

    t1 = cv2.getTickCount() # ...... t2 = cv2.getTickCount() # 计算花费的时间:毫秒 time = (t2-t1) / cv2.getTickFr ...

  8. 【代码学习】PYTHON 抛出异常

    class ShortInputException(Exception): '''你定义的异常类.''' def __init__(self, length, atleast): Exception. ...

  9. GO Range

    Go 语言中 range 关键字用于 for 循环中迭代数组(array).切片(slice).通道(channel)或集合(map)的元素.在数组和切片中它返回元素的索引和索引对应的值,在集合中返回 ...

  10. ES6-const定义常量

    在es5中我们一般将变量名大写来表明这是一个常量,但其实它是可以修改的. 在es6中可以用const来定义常量,它定义的常量不能修改.     const NAME = 'tom';     NAME ...