好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

如何在java中使用Jython

前言:

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

一、Jython是什么

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

二、使用步骤

1.引入依赖

代码如下(示例):

?

1

2

3

4

5

        <dependency>

            <groupId>org.python</groupId>

            <artifactId>jython-standalone</artifactId>

            <version> 2.7 . 0 </version>

        </dependency>

2.调用代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

         //功能:从word中找出对应的加密后的数据,加密算法是hash.md5_crypt

          //原始数据

        List<String> word = new ArrayList<>();

        word.add( "123" );

        word.add( "456" );

        //加密后数据

        List<String> 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<String> result = (List<String>)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脚本

?

1

2

3

4

5

6

7

8

9

10

11

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里,需要在脚本里引入安装路径, 例如:

?

1

2

import sys

sys.path.append(‘D:\tools\Anaconda\lib\site - packages')

或通过代码的方式引入:

?

1

2

interpreter. exec ([ import sys]);

interpreter. exec ([sys.path.append(‘D:\tools\Anaconda\lib\site - packages')]);

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

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

?

1

2

3

4

5

6

7

8

9

   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) 方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

  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文件路径里带了中文。

到此这篇关于如何在java中使用Jython的文章就介绍到这了,更多相关在java中使用Jython内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/RenshenLi/article/details/110574358

查看更多关于如何在java中使用Jython的详细内容...

  阅读:24次