溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在java中使用Jython

發(fā)布時(shí)間:2022-03-01 16:20:16 來(lái)源:億速云 閱讀:144 作者:iii 欄目:開發(fā)技術(shù)

這篇“怎么在java中使用Jython”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“怎么在java中使用Jython”文章吧。

一、Jython是什么

Jython 是 Python 的純 Java 實(shí)現(xiàn)。她無(wú)縫地結(jié)合了 Java 類與 Python,使用戶能以 Python 語(yǔ)言的語(yǔ)法編寫在 Java 虛擬機(jī)上運(yùn)行的 軟件。它的特點(diǎn)有:與相似的 Java 程序相比,Jython 極大的的減少了編程代碼量。Jython 同時(shí)擁有解釋器和編譯器,使其無(wú)需編譯就可以測(cè)試程序代碼。

二、使用步驟

1.引入依賴

代碼如下(示例):

       <dependency>
            <groupId>org.python</groupId>
            <artifactId>jython-standalone</artifactId>
            <version>2.7.0</version>
        </dependency>

2.調(diào)用代碼

        //功能:從word中找出對(duì)應(yīng)的加密后的數(shù)據(jù),加密算法是hash.md5_crypt
         //原始數(shù)據(jù)
        List<String> word = new ArrayList<>();
        word.add("123");
        word.add("456");
        //加密后數(shù)據(jù)
        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);
        //調(diào)用
        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();

輸出結(jié)果:

[&lsquo;word:456, crypt_word:1 11KP074k5L$GkgfZVwByM0FQt4l.KLoh/&rsquo;, &lsquo;word:123, crypt_word:1 11zTxoz1fL$HKSbEyNFHGkLgAHZUTjmz.&rsquo;]

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

三、問(wèn)題

1.報(bào)錯(cuò):ImportError: No module named passlib

報(bào)錯(cuò)提示說(shuō)沒(méi)有安裝passlib庫(kù),則需要導(dǎo)入passlib庫(kù),才能使用from passlib.hash import md5_crypt
linux上可以通過(guò)pip install passlib 命令安裝
windows:例如可以使用spyder執(zhí)行pip install passlib安裝
如果安裝后還是報(bào)錯(cuò),則可能是由于庫(kù)安裝路徑不在path里,需要在腳本里引入安裝路徑,例如:

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

或通過(guò)代碼的方式引入:

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

2.報(bào)錯(cuò):Cannot create PyString with non-byte value

在源碼中可以找到報(bào)錯(cuò)的地方:

  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;
    }

再進(jìn)入 isBytes(string) 方法:

 private static boolean isBytes(String s) {
        int k = s.length();
        if (k == 0) {
            return true;
        } else {
            char c = 0;
            // 每次循環(huán)計(jì)算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);
            }
            // 計(jì)算最后剩下的不足8次的
            while (k > 0) {
                c |= s.charAt(--k);
            }
            // 比較大小
            return c < 0x100;
        }
    }

該方法是對(duì)傳進(jìn)來(lái)的字符串進(jìn)行每個(gè)字符的求或運(yùn)算,最終結(jié)果要小于 0x100,也就是256,也就是說(shuō)每個(gè)字符的大小是不能超過(guò)256的。
而我這里報(bào)錯(cuò)的原因是在創(chuàng)建PythonInterpreter 時(shí)傳入的python文件路徑里帶了中文。

以上就是關(guān)于“怎么在java中使用Jython”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI