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中的对象拷贝示例 python引用传递
Jan 23 Python
win7 下搭建sublime的python开发环境的配置方法
Jun 18 Python
Python中实现对list做减法操作介绍
Jan 09 Python
python中的装饰器详解
Apr 13 Python
基于Python_脚本CGI、特点、应用、开发环境(详解)
May 23 Python
Python使用修饰器执行函数的参数检查功能示例
Sep 26 Python
python将字符串以utf-8格式保存在txt文件中的方法
Oct 30 Python
对pandas中两种数据类型Series和DataFrame的区别详解
Nov 12 Python
Python 3 实现定义跨模块的全局变量和使用教程
Jul 07 Python
opencv导入头文件时报错#include的解决方法
Jul 31 Python
用python画一只可爱的皮卡丘实例
Nov 21 Python
基于logstash实现日志文件同步elasticsearch
Aug 06 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利用iframe实现无刷新文件上传功能的代码
2011/09/29 PHP
总结PHP内存释放以及垃圾回收
2018/03/29 PHP
PHP简单实现正则匹配省市区的方法
2018/04/13 PHP
PHP 代码简洁之道(小结)
2019/10/16 PHP
用jquery来定位
2007/02/20 Javascript
javascript学习笔记(一)基础知识
2014/09/30 Javascript
JavaScript link方法入门实例(给字符串加上超链接)
2014/10/17 Javascript
Markdown与Bootstrap相结合实现图片自适应属性
2016/05/04 Javascript
js实现定时进度条完成后切换图片
2017/01/04 Javascript
基于node.js制作简单爬虫教程
2017/06/29 Javascript
微信小程序点击控件修改样式实例详解
2017/07/07 Javascript
详解前端路由实现与react-router使用姿势
2017/08/07 Javascript
js 发布订阅模式的实例讲解
2017/09/10 Javascript
微信公众号平台接口开发 获取微信服务器IP地址方法解析
2019/08/14 Javascript
[01:45]2014DOTA2 TI预选赛预选赛 战前探营!
2014/05/21 DOTA
Python基于PycURL自动处理cookie的方法
2015/07/25 Python
解决python nohup linux 后台运行输出的问题
2018/05/11 Python
用python实现k近邻算法的示例代码
2018/09/06 Python
PyTorch搭建一维线性回归模型(二)
2019/05/22 Python
python 一篇文章搞懂装饰器所有用法(建议收藏)
2019/08/23 Python
Python基础之字符串常见操作经典实例详解
2020/02/26 Python
Python 日期与时间转换的方法
2020/08/01 Python
python MD5加密的示例
2020/10/19 Python
Django用内置方法实现简单搜索功能的方法
2020/12/18 Python
定义css设备类型-Media Queries图表简介及使用方法
2013/01/21 HTML / CSS
使用CSS3来制作消息提醒框
2015/07/12 HTML / CSS
英国最大的自有市场,比亚马逊便宜:Flubit
2019/03/19 全球购物
Servlet如何得到服务器的信息
2015/12/22 面试题
商务英语毕业生自荐信范文
2013/11/08 职场文书
外贸业务员岗位职责
2013/11/24 职场文书
党员应该树立反腐倡廉的坚定意识思想汇报
2014/09/12 职场文书
2014年精神文明工作总结
2014/12/23 职场文书
财务经理岗位职责
2015/01/31 职场文书
不同意离婚答辩状
2015/05/22 职场文书
音乐会主持人开场白
2015/05/28 职场文书
一次MySQL启动导致的事故实战记录
2021/09/15 MySQL