如何在java中使用Jython
前言:
由于項目中需要用到Java調(diào)用Python的腳本,來實現(xiàn)一些功能,就對jython做了一些了解,通過jython可以實現(xiàn)java對python腳本的調(diào)用。
一、Jython是什么
Jython 是 Python 的純 Java 實現(xiàn)。她無縫地結(jié)合了 Java 類與 Python,使用戶能以 Python 語言的語法編寫在 Java 虛擬機(jī)上運(yùn)行的 軟件。它的特點(diǎn)有:與相似的 Java 程序相比,Jython 極大的的減少了編程代碼量。Jython 同時擁有解釋器和編譯器,使其無需編譯就可以測試程序代碼。
二、使用步驟
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.報錯:ImportError: No module named passlib
報錯提示說沒有安裝passlib庫,則需要導(dǎo)入passlib庫,才能使用from passlib.hash import md5_crypt
linux上可以通過pip install passlib 命令安裝
windows:例如可以使用spyder執(zhí)行pip install passlib安裝
如果安裝后還是報錯,則可能是由于庫安裝路徑不在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.報錯: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;
? ? }再進(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)計算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;
? ? ? ? }
? ? }該方法是對傳進(jìn)來的字符串進(jìn)行每個字符的求或運(yùn)算,最終結(jié)果要小于 0x100,也就是256,也就是說每個字符的大小是不能超過256的。
而我這里報錯的原因是在創(chuàng)建PythonInterpreter 時傳入的python文件路徑里帶了中文。
到此這篇關(guān)于如何在java中使用Jython的文章就介紹到這了,更多相關(guān)在java中使用Jython內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud Feign性能優(yōu)化代碼實例
這篇文章主要介紹了Spring Cloud Feign性能優(yōu)化代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
SpringBoot如何使用Scala進(jìn)行開發(fā)的實現(xiàn)
這篇文章主要介紹了SpringBoot如何使用Scala進(jìn)行開發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
簡單捋捋@RequestParam 和 @RequestBody的使用
這篇文章主要介紹了簡單捋捋@RequestParam 和 @RequestBody的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
SpringBoot實現(xiàn)阿里云短信接口對接的示例代碼
這篇文章主要介紹了SpringBoot實現(xiàn)阿里云短信接口對接的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Spring Boot 開發(fā)私有即時通信系統(tǒng)(WebSocket)
本文利用Spring Boot作為基礎(chǔ)框架,Spring Security作為安全框架,WebSocket作為通信框架,實現(xiàn)點(diǎn)對點(diǎn)聊天和群聊天2017-04-04
SpringBoot實現(xiàn)微信支付接口調(diào)用及回調(diào)函數(shù)(商戶參數(shù)獲取)
本文詳細(xì)介紹了使用SpringBoot實現(xiàn)微信支付接口調(diào)用及回調(diào)函數(shù)的步驟,提供了代碼實現(xiàn)的具體步驟和工具類的創(chuàng)建,感興趣的朋友跟隨小編一起看看吧2024-11-11

