Python基于pyjnius库实现访问java类
程序员文章站
2022-09-24 23:28:22
简介pyjnius是一个用于访问java类的python库。适用场景:极个别的加密算法等内容,用python不方便实现或者实现较耗时,可基于pyjnius把java类当做python库使用。文档:下载...
简介
pyjnius是一个用于访问java类的python库。
适用场景:极个别的加密算法等内容,用python不方便实现或者实现较耗时,可基于pyjnius把java类当做python库使用。
文档:
下载地址:https://pypi.python.org/pypi?%3aaction=search&term=jnius&submit=search
注意jnius的版本管理有点混乱,目前看来选择jniusx比较好。
git地址:
安装
先安装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://*.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 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 . ? - > 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 jnius import autoclass in[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 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 . ? - > 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 jnius import autoclass in[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 jnius import autoclass aesutil = autoclass( 'com.oppo.sso.util.aesutil') email = aesutil.aesencrypt( "hello@126.com", "我是一个加密密钥") print(email)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Git 标签使用详解