亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python基于pyjnius庫實現訪問java類

瀏覽:24日期:2022-07-15 15:58:26

簡介

Pyjnius是一個用于訪問Java類的Python庫。

適用場景:極個別的加密算法等內容,用python不方便實現或者實現較耗時,可基于Pyjnius把java類當做python庫使用。

文檔:http://pyjnius.readthedocs.io/en/latest/installation.html

下載地址:https://pypi.python.org/pypi?%3Aaction=search&term=jnius&submit=search

注意jnius的版本管理有點混亂,目前看來選擇jniusx比較好。

git地址:https://github.com/kivy/pyjnius/blob/master/docs/source/index.rst

安裝

先安裝Java JDK 和JRE、Cython

#pip3 install cython# pip3 install jniusxCollecting jniusxDownloading jniusx - 1.0.5. tar.gzRequirement already satisfied: six >=1.7.0 in /opt/python3.5 / lib / python3.5 / site - packages(from jniusx)Requirement already satisfied: cython in/opt/python3.5 / lib / python3.5 / site - packages(from jniusx)Installing collected packages: jniusxRunning setup.py installfor jniusx...doneSuccessfully installed jniusx - 1.0.5

注意:jnius安裝的坑比較多,請參考http://stackoverflow.com/search?q=jnius

如果出現ImportError,一般是java環境變量或者path沒有配置好。

jnius/jnius.c:4:20: fatal error: Python.h 一般為缺python-dev, yum -y install python-devel

pip 安裝不成功可以嘗試 setup.py方式。

jnius/jnius.c: No such file or directory 需要利用原來保存的clone。

快速入門

hello world 實例:

#!/usr/bin/env python#- * -coding: utf - 8 - * -#jnius_quick2.py# Author Rongzhong Xu 2016 - 08 - 02 wechat:pythontesting# https: //bitbucket.org/china-testing/python-chinese-library/src'''jnius demoTested in python2.7'''from jniusimport autoclassSystem = autoclass(’java.lang.System’)System.out.println(’Hello World’)

堆棧實例:

#!/usr/bin/env python#- * -coding: utf - 8 - * -#jnius_quick1.py# Author Rongzhong Xu 2016 - 08 - 02 wechat:pythontesting# https: //bitbucket.org/china-testing/python-chinese-library/src'''jnius demoTested in python2.7'''from jniusimport autoclassStack = autoclass(’java.util.Stack’)stack = Stack()stack.push(’hello’)stack.push(’world’)print(stack.pop())# -- > ’world’print(stack.pop())# -- > ’hello’

調用java String的hashCode

#!/usr/bin/env python#- * -coding: utf - 8 - * -#jnius_quick3.py# Author Rongzhong Xu 2016 - 08 - 02 wechat:pythontesting# https: //bitbucket.org/china-testing/python-chinese-library/src'''jnius demo: Call java String ’s hashCodeTested in python2.7'''from jniusimport autoclassString = autoclass(’java.lang.String’)print(String('hello').hashCode())

調用jar包

#!python#vi com / sample / Beach.javapackage com.sample;public class Beach {private String name;private String city;public Beach(String name, String city) {this.name = name;this.city = city;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}}#echo Main - Class: Beach > manifest.txt# jar cvfm Test.jar manifest.txt com /sample /*.class*/

測試:

#!python#ipythonPython 3.5.2(default, Nov 7 2016, 18: 53: 51)Type 'copyright', 'credits'or 'license'for more information.IPython 5.1.0--An enhanced Interactive Python.? - > Introduction and overview of IPython’s features.% quickref - > Quick reference.help - > Python ’s own help system.object ? - > Details about ’object’,use ’object??’for extra details.In[2]: #注意要先把jar加載CLASSPATH環境變量。In[3]: from jniusimport autoclassIn[4]: Bla = autoclass(’com.sample.Beach’)In[5]: s = Bla('Tom', 'Shenzhen')In[6]: s.getName()Out[6]: ’Tom’```封裝某模塊的加密機制為python包實例:* 拷貝: com cn org 到新建的臨時目錄* echo Main-Class: AESUtil >manifest.txt* jar cvfm Test.jar manifest.txt *測試代碼:```python# - * -coding: utf - 8 - * -#注意要先把jar加載CLASSPATH環境變量。from jniusimport autoclassAESUtil = autoclass(’com.oppo.sso.util.AESUtil’)email = AESUtil.aesEncrypt('hello@126.com', '我是一個加密密鑰')print(email)# !python# ipythonPython 3.5.2(default, Nov 7 2016, 18: 53: 51)Type 'copyright', 'credits'or 'license'for more information.IPython 5.1.0--An enhanced Interactive Python.? - > Introduction and overview of IPython’s features.% quickref - > Quick reference.help - > Python ’s own help system.object ? - > Details about ’object’,use ’object??’for extra details.In[2]: #注意要先把jar加載CLASSPATH環境變量。In[3]: from jniusimport autoclassIn[4]: Bla = autoclass(’com.sample.Beach’)In[5]: s = Bla('Tom', 'Shenzhen')In[6]: s.getName()Out[6]: ’Tom’```封裝某模塊的加密機制為python包實例:* 拷貝: com cn org 到新建的臨時目錄* echo Main-Class: AESUtil >manifest.txt* jar cvfm Test.jar manifest.txt *測試代碼:```python# - * -coding: utf - 8 - * -#注意要先把jar加載CLASSPATH環境變量。from jniusimport autoclassAESUtil = autoclass(’com.oppo.sso.util.AESUtil’)email = AESUtil.aesEncrypt('hello@126.com', '我是一個加密密鑰')print(email)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚洲欧美日韩高清一区二区一 | 欧美黑人c黑人做人爱视频 欧美黑人vs亚裔videos | 曰韩毛片 | 国产成在线观看免费视频成本人 | 欧美三黑人一级特黄曰皮 | 亚洲第一黄网 | 国产小视频在线观看免费 | 日韩精品一区二三区中文 | 看黄网站在线观看 | 未成人做爰视频www 未成人禁止视频高清在线观看 | 日本性生活片 | 国产免费v片在线看 | 国产aaaaa一级毛片 | 大ji吧快给我别停受不了视频 | 风间由美中文字幕亚洲一区 | 高清免费国产在线观看 | 精品全国在线一区二区 | 亚洲一区成人 | 黄色的网站免费观看 | 亚洲欧美久久精品1区2区 | 色婷婷亚洲五月色综合色 | 成人在线观看视频网站 | 亚洲综合亚洲国产尤物 | 免费看黄色的视频 | 久久精品在线播放 | 国产成人综合网在线播放 | 嫩草免费视频 | 欧美一区二区三区网站 | 欧美不卡一区二区三区免 | 国产精品三级在线观看 | 最新国产在线播放 | 国产主播福利精品一区二区 | 国产污视频在线播放 | 黄色毛片在线播放 | 欧洲美女粗暴交视频 | sese在线 | 精品综合久久久久久8888 | 欧美网站在线观看 | 国产一级大片在线观看 | 国产精品一区二区免费福利视频 | 鲁丝一区二区三区不属 |