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 socket网络编程TCP/IP服务器与客户端通信
Jan 05 Python
Django入门使用示例
Dec 12 Python
python使用Apriori算法进行关联性解析
Dec 21 Python
NumPy 如何生成多维数组的方法
Feb 05 Python
Python 编码规范(Google Python Style Guide)
May 05 Python
使用python进行文本预处理和提取特征的实例
Jun 05 Python
python 3.7.0 安装配置方法图文教程
Aug 27 Python
Python高阶函数、常用内置函数用法实例分析
Dec 26 Python
tensorflow 分类损失函数使用小记
Feb 18 Python
简单了解Python write writelines区别
Feb 27 Python
python 密码学示例——凯撒密码的实现
Sep 21 Python
Python操控mysql批量插入数据的实现方法
Oct 27 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
WINDOWS 2000下使用ISAPI方式安装PHP
2006/09/05 PHP
PHP中的Streams详细介绍
2014/11/12 PHP
php多文件打包下载的实例代码
2017/07/12 PHP
Laravel中的Auth模块详解
2017/08/17 PHP
解决在laravel中auth建立时候遇到的问题
2019/10/15 PHP
ext checkboxgroup 回填数据解决
2009/08/21 Javascript
JQuery 网站换肤功能实现代码
2009/11/02 Javascript
基于jquery的代码显示区域自动拉长效果
2011/12/07 Javascript
Uglifyjs(JS代码优化工具)入门 安装使用
2020/04/13 Javascript
在服务端(Page.Write)调用自定义的JS方法详解
2013/08/09 Javascript
javascript制作的网页侧边弹出框思路及实现代码
2014/05/21 Javascript
JS动画效果打开、关闭层的实现方法
2015/05/09 Javascript
jquery实现点击页面回到顶部
2016/11/23 Javascript
微信小程序 本地数据读取实例
2017/04/27 Javascript
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法
2017/06/17 Javascript
使用pkg打包Node.js应用的方法步骤
2018/10/19 Javascript
KOA+egg.js集成kafka消息队列的示例
2018/11/09 Javascript
谈谈JavaScript中super(props)的重要性
2019/02/12 Javascript
vuejs实现下拉框菜单选择
2020/10/23 Javascript
[37:02]OG vs INfamous 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
Python对列表排序的方法实例分析
2015/05/16 Python
python中matplotlib实现最小二乘法拟合的过程详解
2017/07/11 Python
Python编程求质数实例代码
2018/01/31 Python
浅谈Python脚本开头及导包注释自动添加方法
2018/10/27 Python
Python实现简单层次聚类算法以及可视化
2019/03/18 Python
选择python进行数据分析的理由和优势
2019/06/25 Python
Python做图像处理及视频音频文件分离和合成功能
2020/11/24 Python
国贸专业个人求职信范文
2014/01/08 职场文书
妇联领导班子剖析材料
2014/08/21 职场文书
领导班子遵守党的政治纪律情况对照检查材料
2014/09/26 职场文书
工作检讨书怎么写
2014/10/10 职场文书
房地产销售经理岗位职责
2015/02/02 职场文书
2015年大学迎新工作总结
2015/07/16 职场文书
Python如何识别银行卡卡号?
2021/06/10 Python
2021年国漫热度排行前十,完美世界上榜,第四是美国动画作品
2022/03/18 国漫
vue中data里面的数据相互使用方式
2022/06/05 Vue.js