jython实现java运行python代码

Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现。最近的一个项目需要将python代码转换成java实现,所以用了一下jython。

试用了jython的2.7的版本发现运行一直出错,不知道是不是版本的原因,但是2.5的版本还是可以的。

第一步,先来一个简单的(先确定你已经下载添加了对应的jar包)

java代码:

PythonInterpreter interpreter = new PythonInterpreter();  
interpreter.execfile(
"/home/桌面/PycharmProjects/first/1.py");

python代码:

print("hello jython")

输出:

第二步:调用方法(不含参数)

java代码:

PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/home/ybf/PycharmProjects/first/1.py");
PyFunction func_first = (PyFunction)interpreter.get("first",PyFunction.class);
PyFunction func_second= (PyFunction)interpreter.get("second",PyFunction.class);
PyObject pyobj = func_second.__call__();
System.out.println(pyobj);

python代码:

def first():
print("first ...........")

first()

def second():
a=100
b=50
return a+b

输出:

第三步:调用方法(含参数)

java代码:

PythonInterpreter interpreter = new PythonInterpreter(); 
interpreter.execfile("/home/ybf/PycharmProjects/first/1.py"); 

PyFunction func_third= (PyFunction)interpreter.get("third",PyFunction.class); 

PyObject pyobj = func_third.__call__(new PyInteger(4), new PyInteger(2));
System.out.println(pyobj);

python代码:

def third(a,b):
c=sub(a,b)
d=sub(b,a)
return c*d

def sub(a,b):
return a-b

输出:

第四步:关于中文处理,这是一个很麻烦的方面,大家可以看下面的例子

java代码:

String a = "你好";  
PyFunction func= (PyFunction)interpreter.get("word_process",PyFunction.class);
PyObject pyobj = func.__call__(new PyString(a));  
System.out.println(pyobj.toString());

python代码:

def word_process(a):
    if a=="你好":                                                                                                           
        print(True)
    else:
        print(False)
    print(a)
    return a 

结果:

这里可以看到在Python里面输出在eclipse输出的是?,其实输出的是“你好”,但是因为平台的原因所以显示?(个人的理解),而且大家可以发现在python中的“你好”不等于java里面的“你好”,这方面本人还不知道,不知道有没有大佬知道,怎样处理才返回True,

str(a).encode('utf-8')=="你好".encode("utf-8")这样返回的也是False

第五步:打开txt文本

这里注意python里面的代码,如下:

 

f=open('src/dic/v.txt','rt')#注意文件路径
while(True):
    line=f.readline()
    if not line:
        break
    print(line)

 

如果这里使用 with open('src/dic/v.txt','rt') as f 但在eclipse报错如下:

 

 今天写到这里,有时间再更新 

转载于:https://www.cnblogs.com/ybf-yyj/p/8344139.html

  • 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、付费专栏及课程。

余额充值