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. Clausen Functions (and related series, functions, integrals)

    Since the Clausen functions are intimately related to a number of other important special functions, ...

  2. robotframe常用的类库、对应的方法和属性

    robotframe常用的类库.对应的方法和属性

  3. gitlab的搭建与汉化

    gitlab的搭建:内存最好2G以上 yum -y install curl unzip policycoreutils git wget         安装相关依赖包 所有gitlab rpm包的 ...

  4. DL4J之CNN对今日头条文本分类

    一.数据集介绍 数据来源:今日头条客户端 数据格式如下: 6551700932705387022_!_101_!_news_culture_!_京城最值得你来场文化之旅的博物馆_!_保利集团,马未都, ...

  5. PyQt5剪切板操作

    1.使用剪切板import sys,mathfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import ...

  6. Golang核心编程

    源码地址: https://github.com/mikeygithub/GoCode 第1章 1Golang 的学习方向 Go 语言,我们可以简单的写成 Golang 1.2Golang 的应用领域 ...

  7. Executor、Executors、ExecutorService多线程操作

    Executor:一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable command),该方法接收一个Runable实例,它用来执行一 ...

  8. 【原】python总结

    python3浅拷贝和深拷贝:https://www.jianshu.com/p/c7e72fcad407

  9. tf.app.run()的作用

    tf.app.run() 如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test) 如果你的代码中的入口函数叫main(), ...

  10. 在自定义的widget中引入图表后,运行时出现TypeError #1009错误

    本人网上查找了很多资料,其中大部分都是关于Flash中的动画效果问题,与这里的问题关系型不太大.故把问题的解决方法写在这里,与人方便,与己方便. 方法一: 1.在自定义的widget中添加如下两个方法 ...