Python基于pyjnius库实现访问java类


Posted in Python onJuly 31, 2020

简介

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 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?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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 文件操作api(文件操作函数)
Aug 28 Python
python中range()与xrange()用法分析
Sep 21 Python
python中的迭代和可迭代对象代码示例
Dec 27 Python
python读取word文档,插入mysql数据库的示例代码
Nov 07 Python
Python mutiprocessing多线程池pool操作示例
Jan 30 Python
Django Sitemap 站点地图的实现方法
Apr 29 Python
Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
Jun 25 Python
Python-jenkins 获取job构建信息方式
May 12 Python
Python如何发送与接收大型数组
Aug 07 Python
python实现图片转换成素描和漫画格式
Aug 19 Python
协程Python 中实现多任务耗资源最小的方式
Oct 19 Python
python读取mat文件生成h5文件的实现
Jul 15 Python
Python如何将字符串转换为日期
Jul 31 #Python
Python在字符串中处理html和xml的方法
Jul 31 #Python
python中selenium库的基本使用详解
Jul 31 #Python
Python过滤序列元素的方法
Jul 31 #Python
python中的django是做什么的
Jul 31 #Python
如何基于python把文字图片写入word文档
Jul 31 #Python
django教程如何自学
Jul 31 #Python
You might like
ThinkPHP中create()方法自动验证表单信息
2017/04/28 PHP
RGB颜色值转HTML十六进制(HEX)代码的JS函数
2009/04/25 Javascript
AppBaseJs 类库 网上常用的javascript函数及其他js类库写的
2010/03/04 Javascript
Ajax异步提交表单数据的说明及方法实例
2013/06/22 Javascript
jquery序列化表单去除指定元素示例代码
2014/04/10 Javascript
jQuery使用之标记元素属性用法实例
2015/01/19 Javascript
纯javascript实现四方向文本无缝滚动效果
2015/06/16 Javascript
JavaScript中Boolean对象的属性解析
2015/10/21 Javascript
详解JavaScript异步编程中jQuery的promise对象的作用
2016/05/03 Javascript
Bootstrap select多选下拉框实现代码
2016/12/23 Javascript
利用Node.js编写跨平台的spawn语句详解
2017/02/12 Javascript
JS优化与惰性载入函数实例分析
2017/04/06 Javascript
详解基于node.js的脚手架工具开发经历
2019/01/28 Javascript
微信小程序通过js实现瀑布流布局详解
2019/08/28 Javascript
JS实现的进制转换,浮点数相加,数字判断操作示例
2019/11/09 Javascript
Python中利用sorted()函数排序的简单教程
2015/04/27 Python
python实现批量监控网站
2016/09/09 Python
深入理解python对json的操作总结
2017/01/05 Python
python实时监控cpu小工具
2018/06/21 Python
使用pycharm设置控制台不换行的操作方法
2019/01/19 Python
使用 Python 处理 JSON 格式的数据
2019/07/22 Python
Python定时从Mysql提取数据存入Redis的实现
2020/05/03 Python
Django使用django-simple-captcha做验证码的实现示例
2021/01/07 Python
纯css3实现的动画按钮的实例教程
2014/11/17 HTML / CSS
HTML5 Canvas 起步(2) - 路径
2009/05/12 HTML / CSS
巴黎一票通:The Paris Pass
2018/02/10 全球购物
Shopee新加坡:东南亚与台湾电商平台
2019/01/25 全球购物
大四学生毕业自荐信
2013/11/07 职场文书
4s店机修工岗位职责
2013/12/20 职场文书
刑事代理授权委托书
2014/09/17 职场文书
公务员学习习总书记“三严三实”思想汇报
2014/09/19 职场文书
2014幼儿园小班工作总结
2014/11/10 职场文书
2014年工商所工作总结
2014/12/09 职场文书
国家助学金感谢信
2015/01/21 职场文书
CSS控制继承中的height能变为可继承吗
2022/06/10 HTML / CSS
MySql统计函数COUNT的具体使用详解
2022/08/14 MySQL