简介
Pyjnius是一个用于访问Java类的Python库。
适用场景:极个别的加密算法等内容,用python不方便实现或者实现较耗时,可基于Pyjnius把java类当做python库使用。
文档:http://pyjnius.readthedocs.io/en/latest/installation.html
下载地址:https://pypi.python.org/pypi"_blank" href="https://github.com/kivy/pyjnius/blob/master/docs/source/index.rst" rel="external nofollow" >https://github.com/kivy/pyjnius/blob/master/docs/source/index.rst
安装
先安装Java JDK 和JRE、Cython
# pip3 install cython # pip3 install jniusx Collecting jniusx Downloading jniusx - 1.0.5. tar.gz Requirement already satisfied: six >= 1.7.0 in /opt/python 3.5 / lib / python3.5 / site - packages( from jniusx) Requirement already satisfied: cython in /opt/python 3.5 / lib / python3.5 / site - packages( from jniusx) Installing collected packages: jniusx Running setup.py install for jniusx...done Successfully installed jniusx - 1.0.5
注意:jnius安装的坑比较多,请参考http://stackoverflow.com/search"htmlcode">
#!/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 demo Tested in python2.7 "" " from jnius import autoclass System = 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 demo Tested in python2.7 "" " from jnius import autoclass Stack = 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 hashCode Tested in python2.7 "" " from jnius import autoclass String = autoclass('java.lang.String') print(String("hello").hashCode())
调用jar包
#!python # vi com / sample / Beach.java package 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 # ipython Python 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 . "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 jnius import autoclass AESUtil = autoclass( 'com.oppo.sso.util.AESUtil') email = AESUtil.aesEncrypt( "hello@126.com", "我是一个加密密钥") print(email)# !python # ipython Python 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 . "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 jnius import autoclass AESUtil = autoclass( 'com.oppo.sso.util.AESUtil') email = AESUtil.aesEncrypt( "hello@126.com", "我是一个加密密钥") print(email)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Python基于pyjnius库实现访问java类”评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]