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脚本操作文件的小示例分享
Aug 27 Python
Python下的Softmax回归函数的实现方法(推荐)
Jan 26 Python
Python基于动态规划算法解决01背包问题实例
Dec 06 Python
python opencv之分水岭算法示例
Feb 24 Python
python批量读取txt文件为DataFrame的方法
Apr 03 Python
pandas 空的dataframe 插入列名的示例
Oct 30 Python
Python3 SSH远程连接服务器的方法示例
Dec 29 Python
Python3按一定数据位数格式处理bin文件的方法
Jan 24 Python
Python向excel中写入数据的方法
May 05 Python
在PyCharm的 Terminal(终端)切换Python版本的方法
Aug 02 Python
python生成随机数、随机字符、随机字符串
Apr 06 Python
Python+DeOldify实现老照片上色功能
Jun 21 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目录导航文件代码
2006/10/09 PHP
vBulletin HACK----关于排版的两个HACK
2006/10/09 PHP
PHP新手上路(二)
2006/10/09 PHP
php中通过smtp发邮件的类,测试通过
2007/01/22 PHP
PHP正则表达式之定界符和原子介绍
2012/10/05 PHP
ThinkPHP查询返回简单字段数组的方法
2014/08/25 PHP
Laravel重写用户登录简单示例
2016/10/08 PHP
js 获取子节点函数 (兼容FF与IE)
2010/04/18 Javascript
javascript 循环读取JSON数据的代码
2010/07/17 Javascript
jQuery点击弹出下拉菜单的小例子
2013/08/01 Javascript
js showModalDialog参数的使用详解
2014/01/07 Javascript
Jquery实现自定义tooltip示例代码
2014/02/12 Javascript
javascript中异常处理案例(推荐)
2016/10/03 Javascript
jQuery滑动到底部加载下一页数据的实例代码
2017/05/22 jQuery
jQuery鼠标移动图片上实现放大效果
2017/06/25 jQuery
使用Bootstrap和Vue实现用户信息的编辑删除功能
2017/10/25 Javascript
vue.js实现的绑定class操作示例
2018/07/06 Javascript
Angular-UI Bootstrap组件实现警报功能
2018/07/16 Javascript
基于node.js实现爬虫的讲解
2019/02/18 Javascript
JQueryDOM之样式操作
2019/03/27 jQuery
[45:25]完美世界DOTA2联赛循环赛 PXG vs IO 第一场 11.06
2020/11/09 DOTA
Django中几种重定向方法
2015/04/28 Python
Python装饰器实现几类验证功能做法实例
2017/05/18 Python
Python实现将一个正整数分解质因数的方法分析
2017/12/14 Python
python 文件转成16进制数组的实例
2018/07/09 Python
Ubuntu下Python2与Python3的共存问题
2018/10/31 Python
详解python中eval函数的作用
2019/10/22 Python
python不相等的两个字符串的 if 条件判断为True详解
2020/03/12 Python
加拿大大码女装购物网站:Penningtons
2020/12/26 全球购物
北京泡泡网网络有限公司.net面试题
2012/07/17 面试题
如何写一封打动人心的求职信
2014/02/17 职场文书
出生医学证明书
2014/09/15 职场文书
公安领导班子四风问题个人整改措施思想汇报
2014/10/09 职场文书
安全先进班组材料
2014/12/26 职场文书
婚姻出轨保证书
2015/05/08 职场文书
ubuntu安装jupyter并设置远程访问的实现
2022/03/31 Python