如何在java中使用Jython
前言:
由于項(xiàng)目中需要用到Java調(diào)用Python的腳本,來實(shí)現(xiàn)一些功能,就對jython做了一些了解,通過jython可以實(shí)現(xiàn)java對python腳本的調(diào)用。
一、Jython是什么
Jython 是 Python 的純 Java 實(shí)現(xiàn)。她無縫地結(jié)合了 Java 類與 Python,使用戶能以 Python 語言的語法編寫在 Java 虛擬機(jī)上運(yùn)行的 軟件。它的特點(diǎn)有:與相似的 Java 程序相比,Jython 極大的的減少了編程代碼量。Jython 同時(shí)擁有解釋器和編譯器,使其無需編譯就可以測試程序代碼。
二、使用步驟
1.引入依賴
代碼如下(示例):
? ??? ?<dependency> ? ? ? ? ? ? <groupId>org.python</groupId> ? ? ? ? ? ? <artifactId>jython-standalone</artifactId> ? ? ? ? ? ? <version>2.7.0</version> ? ? ? ? </dependency>
2.調(diào)用代碼
?? ??? ?//功能:從word中找出對應(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三、問題
1.報(bào)錯(cuò):ImportError: No module named passlib
報(bào)錯(cuò)提示說沒有安裝passlib庫,則需要導(dǎo)入passlib庫,才能使用from passlib.hash import md5_crypt
linux上可以通過pip install passlib 命令安裝
windows:例如可以使用spyder執(zhí)行pip install passlib安裝
如果安裝后還是報(bào)錯(cuò),則可能是由于庫安裝路徑不在path里,需要在腳本里引入安裝路徑,例如:
import sys sys.path.append(‘D:\tools\Anaconda\lib\site-packages')
或通過代碼的方式引入:
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;
? ? ? ? }
? ? }該方法是對傳進(jìn)來的字符串進(jìn)行每個(gè)字符的求或運(yùn)算,最終結(jié)果要小于 0x100,也就是256,也就是說每個(gè)字符的大小是不能超過256的。
而我這里報(bào)錯(cuò)的原因是在創(chuàng)建PythonInterpreter 時(shí)傳入的python文件路徑里帶了中文。
到此這篇關(guān)于如何在java中使用Jython的文章就介紹到這了,更多相關(guān)在java中使用Jython內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot在生產(chǎn)快速禁用Swagger2的方法步驟
這篇文章主要介紹了SpringBoot在生產(chǎn)快速禁用Swagger2的方法步驟,使用注解關(guān)閉Swagger2,避免接口重復(fù)暴露,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-12-12
Spring Boot集成Mybatis的實(shí)例代碼(簡潔版)
這篇文章主要介紹了Spring Boot集成Mybatis簡潔版的教程,需要的朋友可以參考下2018-02-02
java:程序包javafx.geometry不存在問題及解決
這篇文章主要介紹了java:程序包javafx.geometry不存在問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Springboot如何同時(shí)裝配兩個(gè)相同類型數(shù)據(jù)庫
這篇文章主要介紹了Springboot如何同時(shí)裝配兩個(gè)相同類型數(shù)據(jù)庫,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

