在java中调用python方法

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

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

view plain
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

view plain
def adder(a, b):
return a + b

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

java类代码 FirstJavaScript:

view plain
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);//adder python函数名
int a = 2010, b = 2 ;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));//传值,a b
System.out.println("anwser = " + pyobj.toString());
}//main
}

得到的结果是:anwser = 2012

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

view plain
#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类,调用这个脚本:

view plain
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
}

得到的结果是:

view plain
hello
[3, 5, 2, 0, 6]
[0, 2, 3, 5, 6]
[0, 2, 3, 5, 6, 0]
2
3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java调用Python代码有多种方法,其一种是使用JythonJython是一个Java平台上的Python解释器,它允许在Java程序嵌入Python代码,并且可以在Java代码直接调用Python模块和函数。 以下是使用JythonJava调用Python代码的基本步骤: 1. 下载并安装Jython 你可以从Jython官方网站下载Jython安装包,并按照安装向导进行安装。 2. 创建Python脚本 编写一个Python脚本,例如hello.py: ``` def say_hello(name): print("Hello, " + name + "!") ``` 3. 在Java代码加载并调用Python模块 在Java代码使用JythonPythonInterpreter类加载并调用Python模块和函数,例如: ``` import org.python.util.PythonInterpreter; public class Main { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("hello.py"); interpreter.get("say_hello").__call__("John"); } } ``` 在上面的代码,我们首先创建了一个PythonInterpreter对象,然后使用它的execfile方法加载Python脚本文件。接着,我们使用interpreter.get方法获取Python模块的say_hello函数,并使用__call__方法调用该函数,并传递一个参数。 当你运行Java程序时,你应该能够在控制台上看到输出:Hello, John! 需要注意的是,Jython并不是Python的完全实现,它只支持Python 2.7的语法和一些标准库。如果你的Python代码使用了不受支持的语法或库,那么你需要考虑其他的方法来在Java调用Python代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值