如何在java中使用Jython
前言:
由于項(xiàng)目中需要用到Java調(diào)用Python的腳本,來(lái)實(shí)現(xiàn)一些功能,就對(duì)jython做了一些了解,通過(guò)jython可以實(shí)現(xiàn)java對(duì)python腳本的調(diào)用。
一、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é)果:
[‘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
三、問(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的文章就介紹到這了,更多相關(guān)在java中使用Jython內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java開(kāi)啟新線程并傳參方法代碼實(shí)現(xiàn)
這篇文章主要介紹了Java開(kāi)啟新線程并傳參方法代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04SpringBoot在生產(chǎn)快速禁用Swagger2的方法步驟
這篇文章主要介紹了SpringBoot在生產(chǎn)快速禁用Swagger2的方法步驟,使用注解關(guān)閉Swagger2,避免接口重復(fù)暴露,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-12-12Spring Boot集成Mybatis的實(shí)例代碼(簡(jiǎn)潔版)
這篇文章主要介紹了Spring Boot集成Mybatis簡(jiǎn)潔版的教程,需要的朋友可以參考下2018-02-02java:程序包javafx.geometry不存在問(wèn)題及解決
這篇文章主要介紹了java:程序包javafx.geometry不存在問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Springboot如何同時(shí)裝配兩個(gè)相同類型數(shù)據(jù)庫(kù)
這篇文章主要介紹了Springboot如何同時(shí)裝配兩個(gè)相同類型數(shù)據(jù)庫(kù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11