一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

在java中使用Jython代码示例

时间:2022-03-01 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下在java中使用Jython代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

前言:

由于项目中需要用到Java调用Python的脚本,来实现一些功能,就对jython做了一些了解,通过jython可以实现java对python脚本的调用。

一、Jython是什么

Jython 是 Python 的纯 Java 实现。她无缝地结合了 Java 类与 Python,使用户能以 Python 语言的语法编写在 Java 虚拟机上运行的 软件。它的特点有:与相似的 Java 程序相比,Jython 极大的的减少了编程代码量。Jython 同时拥有解释器和编译器,使其无需编译就可以测试程序代码。

二、使用步骤

1.引入依赖

代码如下(示例):

       
            org.python
            jython-standalone
            2.7.0
        

2.调用代码

        //功能:从word中找出对应的加密后的数据,加密算法是hash.md5_crypt
         //原始数据
        List word = new ArrayList<>();
        word.add("123");
        word.add("456");
        //加密后数据
        List cryptWord = new ArrayList<>();
        cryptWord.add("$1$KP074k5L$GkgfZVwByM0FQt4l.KLoh/");
        cryptWord.add("$1$zTxoz1fL$HKSbEyNFHGkLgAHZUTjmz.");

        String pythonFilePath = "jython_test.py";
        String pythonFileMethod = "verify";

        PythonInterpreter interpreter = new PythonInterpreter();
        ClassPathResource resource = new ClassPathResource(pythonFilePath);
        InputStream inputStream = resource.getInputStream();
        interpreter.execfile(inputStream);
        PyFunction verify = interpreter.get(pythonFileMethod, PyFunction.class);
        //调用
        PyObject pyObject = verify.__call__(new PyList(word), new PyList(cryptWord));
        List result = (List)pyObject.__tojava__(List.class);
        System.out.println(result);

        interpreter.close();

输出结果:

[‘word:456, crypt_word:1 11KP074k5L$GkgfZVwByM0FQt4l.KLoh/’, ‘word:123, crypt_word:1 11zTxoz1fL$HKSbEyNFHGkLgAHZUTjmz.’]

2.python脚本

from passlib.hash import md5_crypt

def verify(word,crypt_word):
    result=[]
    for crypt_w in crypt_word:
        for w in word:
            if md5_crypt.verify(w, crypt_w):
                item = 'word:{}, crypt_word:{}'.format(w,crypt_w)
                result.append(item)
                break
    return result

三、问题

1.报错:ImportError: No module named passlib

报错提示说没有安装passlib库,则需要导入passlib库,才能使用from passlib.hash import md5_crypt

linux上可以通过pip install passlib命令安装

windows:例如可以使用spyder执行pip install passlib安装

如果安装后还是报错,则可能是由于库安装路径不在path里,需要在脚本里引入安装路径,例如:

import sys
sys.path.append(‘D:toolsAnacondalibsite-packages')

或通过代码的方式引入:

interpreter.exec(“import sys”);
interpreter.exec(“sys.path.append(‘D:toolsAnacondalibsite-packages')”);

2.报错:Cannot create PyString with non-byte value

在源码中可以找到报错的地方:

  public PyString(PyType subType, String string) {
        super(subType);
        if (string == null) {
            throw new IllegalArgumentException("Cannot create PyString from null");
        } else if (!isBytes(string)) {
            throw new IllegalArgumentException("Cannot create PyString with non-byte value");
        }
        this.string = string;
    }

再进入 isBytes(string) 方法:

 private static boolean isBytes(String s) {
        int k = s.length();
        if (k == 0) {
            return true;
        } else {
            char c = 0;
            // 每次循环计算8次
            while (k > 8) {
                c |= s.charAt(--k);
                c |= s.charAt(--k);
                c |= s.charAt(--k);
                c |= s.charAt(--k);
                c |= s.charAt(--k);
                c |= s.charAt(--k);
                c |= s.charAt(--k);
                c |= s.charAt(--k);
            }
            // 计算最后剩下的不足8次的
            while (k > 0) {
                c |= s.charAt(--k);
            }
            // 比较大小
            return c < 0x100;
        }
    }

该方法是对传进来的字符串进行每个字符的求或运算,最终结果要小于 0x100,也就是256,也就是说每个字符的大小是不能超过256的。

而我这里报错的原因是在创建PythonInterpreter时传入的python文件路径里带了中文。

热门栏目