在java中调用python方法

简介: 1、http://sourceforge.net/projects/jython/下载jython包,把其中的jython.jar添加到工程目录   示例: 1、摘自:http://blog.

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.   
  3. import org.python.util.PythonInterpreter;  
  4.   
  5. import java.io.*;  
  6. import static java.lang.System.*;  
  7. public class FirstJavaScript  
  8. {  
  9.  public static void main(String args[])  
  10.  {  
  11.     
  12.   PythonInterpreter interpreter = new PythonInterpreter();  
  13.   interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");  
  14.   interpreter.exec("print days[1];");  
  15.     
  16.     
  17.  }//main  
  18. }  


这样得到的结果是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.   
  3. import org.python.core.PyFunction;  
  4. import org.python.core.PyInteger;  
  5. import org.python.core.PyObject;  
  6. import org.python.util.PythonInterpreter;  
  7.   
  8. import java.io.*;  
  9. import static java.lang.System.*;  
  10. public class FirstJavaScript  
  11. {  
  12.     public static void main(String args[])  
  13.     {  
  14.           
  15.         PythonInterpreter interpreter = new PythonInterpreter();  
  16.         interpreter.execfile("C:\\Python27\\programs\\my_utils.py");  
  17.         PyFunction func = (PyFunction)interpreter.get("adder",PyFunction.class);  
  18.   
  19.         int a = 2010, b = 2 ;  
  20.         PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));  
  21.         System.out.println("anwser = " + pyobj.toString());  
  22.   
  23.   
  24.     }//main  
  25. }  

得到的结果是:anwser = 2012

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

建立脚本inputpy

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


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

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


得到的结果是:

 
  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  


目录
相关文章
|
14天前
|
Python
python魔法方法如何应用
【4月更文挑战第12天】这个Python示例展示了类继承和方法重写。`Student`类继承自`Person`,并覆盖了`say_hello`方法。通过`super().__init__(name)`调用父类的`__init__`初始化`name`属性,`Student`添加了`age`属性,并在重写的`say_hello`中使用。创建`Student`实例`student`并调用其`say_hello`,输出定制的问候信息。
20 1
|
5天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
31 3
|
1天前
|
Java
Java 与垃圾回收有关的方法
Java 与垃圾回收有关的方法
|
2天前
|
存储 Java 测试技术
一文搞清楚Java中的方法、常量、变量、参数
在JVM的运转中,承载的是数据,而数据的一种变现形式就是“量”,量分为:**常量与变量**,我们在数学和物理学中已经接触过变量的概念了,在Java中的变量就是在程序运行过程中可以改变其值的量。
13 0
|
3天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
21 0
|
6天前
|
存储 Java
Java动态转发代理IP的实现方法
Java动态转发代理IP的实现方法
22 11
|
7天前
|
存储 关系型数据库 MySQL
Python搭建代理IP池实现存储IP的方法
Python搭建代理IP池实现存储IP的方法
|
7天前
|
Python
Python动态IP代理防止被封的方法
Python动态IP代理防止被封的方法
|
8天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)
|
8天前
|
数据采集 存储 安全
python检测代理ip是否可用的方法
python检测代理ip是否可用的方法