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实现2014火车票查询代码分享
Jan 10 Python
详细探究Python中的字典容器
Apr 14 Python
django通过ajax发起请求返回JSON格式数据的方法
Jun 04 Python
浅谈python中scipy.misc.logsumexp函数的运用场景
Jun 23 Python
Python多进程入门、分布式进程数据共享实例详解
Jun 03 Python
使用Python轻松完成垃圾分类(基于图像识别)
Jul 09 Python
Python pandas库中的isnull()详解
Dec 26 Python
python下载卫星云图合成gif的方法示例
Feb 18 Python
Python基于codecs模块实现文件读写案例解析
May 11 Python
python中前缀运算符 *和 **的用法示例详解
May 28 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 Python
教你怎么用Python实现GIF动图的提取及合成
Jun 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
php中计算时间差的几种方法
2009/12/31 PHP
PHP面向对象程序设计方法实例详解
2016/12/24 PHP
JavaScript prototype对象的属性说明
2010/03/13 Javascript
jquery获取tr并更改tr内容示例代码
2014/02/13 Javascript
jquery选择器原理介绍($()使用方法)
2014/03/25 Javascript
jQuery标签编辑插件Tagit使用指南
2015/04/21 Javascript
使用Object.defineProperty实现简单的js双向绑定
2016/04/15 Javascript
下雪了 javascript实现雪花飞舞
2020/08/02 Javascript
javascript中JSON.parse()与eval()解析json的区别
2016/05/19 Javascript
Seajs是什么及sea.js 由来,特点以及优势
2016/10/13 Javascript
获取layer.open弹出层的返回值方法
2018/08/20 Javascript
详解angular2 控制视图的封装模式
2018/12/27 Javascript
JavaScript学习笔记之数组基本操作示例
2019/01/09 Javascript
js实现简单掷骰子效果
2019/10/24 Javascript
[02:32]DOTA2亚洲邀请赛 VG战队巡礼
2015/02/03 DOTA
Django为窗体加上防机器人的验证码功能过程解析
2019/08/14 Python
Python序列对象与String类型内置方法详解
2019/10/22 Python
python3实现在二叉树中找出和为某一值的所有路径(推荐)
2019/12/26 Python
Python Pandas 对列/行进行选择,增加,删除操作
2020/05/17 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
2020/07/07 Python
CSS3属性box-shadow使用详细教程
2012/01/21 HTML / CSS
extern是什么意思
2016/03/10 面试题
现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供reset
2012/11/09 面试题
机关会计岗位职责
2014/04/08 职场文书
入股协议书范本
2014/04/14 职场文书
产品售后服务承诺书
2014/05/21 职场文书
平面设计专业求职信
2014/08/09 职场文书
新兵入伍心得体会
2014/09/04 职场文书
解放思想演讲稿
2014/09/11 职场文书
个人简历自我评价怎么写
2015/03/10 职场文书
2015医院个人工作总结范文
2015/05/21 职场文书
环保建议书作文400字
2015/09/14 职场文书
如何书写邀请函?
2019/06/24 职场文书
Apache压力测试工具的安装使用
2021/03/31 Servers
python实现语音常用度量方法的代码详解
2021/05/25 Python
Python集合的基础操作
2021/11/01 Python