在java中调用python方法

本文介绍了三种Java调用Python的方式:直接执行Python语句、调用本地Python脚本中的函数及直接执行Python脚本,并提供了详细的代码示例。

转载自:http://blog.sina.com.cn/s/blog_64e467d60100uhls.html

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


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  

然后建立一个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 20102  
  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.  
  6.  
### 在 Java调用 Python 函数的方法Java调用 Python 函数可以通过多种方式实现,具体取决于项目需求、性能考量以及 Python 脚本的复杂度。 #### 1. 使用 `Runtime.getRuntime().exec()` 调用 Python 脚本 该方法通过 Java 的 `Runtime` 类执行系统命令,从而运行 Python 脚本。这种方式适用于简单的脚本调用,并能捕获脚本的输出结果。需要注意的是,这种方法依赖于系统环境和 Python 解释器的安装路径。 示例代码如下: ```java Process process = Runtime.getRuntime().exec("python script.py"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } ``` 该方式适用于小数据量处理和脚本执行时间较短的场景 [^2]。 #### 2. 使用 `PythonInterpreter` 执行 Python 语句 通过 `PythonInterpreter` 类可以直接在 Java 代码中嵌入 Python 语句,适用于需要在 Java 中直接执行 Python 逻辑的场景。该方法依赖于 Jython 环境,JythonPythonJava 实现,允许 Python 代码与 Java 类库交互。 ```java import org.python.util.PythonInterpreter; public class PythonInJava { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("print('Hello from Python')"); } } ``` 使用该方式时,如果 Python 脚本依赖第三方库,则需要提前导入相关模块或使用兼容的 Jython 环境 [^3]。 #### 3. 使用 JPype 调用 Java 方法Python 调用 Java) 虽然这不是 Java 调用 Python 的直接方式,但值得注意的是,JPype 允许 Python 代码调用 Java 类和方法。这在需要双向交互的项目中非常有用。 ```python import jpype jvmPath = jpype.getDefaultJVMPath() jpype.startJVM(jvmPath, "-ea", "-Djava.class.path=example.jar") javaClass = jpype.JPackage("com.example").MyClass() result = javaClass.myMethod("input") jpype.shutdownJVM() ``` 此方法适用于 Python 代码中需要调用 Java API 的场景 [^4]。 ### 适用场景对比 - **Runtime.exec**:适用于独立脚本执行,不涉及复杂数据交互。 - **PythonInterpreter**:适用于嵌入式 Python 逻辑,且对第三方库依赖较少的场景。 - **JPype**:适用于 Python 需要调用 Java 方法的反向交互场景。 ###
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值