Python基于pyjnius库实现访问java类

 更新时间:2020年07月31日 14:31:39   作者:Tracydzf  
这篇文章主要介绍了Python基于pyjnius库实现访问java类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

简介

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)

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

相关文章

  • Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法

    Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法

    这篇文章主要介绍了Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Flask路由尾部有没有斜杠有什么区别

    Flask路由尾部有没有斜杠有什么区别

    这篇文章主要介绍了Flask路由尾部加不加斜杠有什么区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • Python数据分析之 Matplotlib 折线图绘制

    Python数据分析之 Matplotlib 折线图绘制

    这篇文章主要介绍了Python数据分析之 Matplotlib 折线图绘制,在数据分析中,数据可视化也非常重要,下文通过数据分析展开对折线图的绘制,需要的小伙伴可以参考一下
    2022-05-05
  • Python文件处理

    Python文件处理

    这篇文章主要介绍了Python文件处理的相关资料,需要的朋友可以参考下
    2016-02-02
  • python基于C/S模式实现聊天室功能

    python基于C/S模式实现聊天室功能

    这篇文章主要为大家详细介绍了python基于C/S模式实现聊天室功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • 一文带你了解Python与svg之间的操作

    一文带你了解Python与svg之间的操作

    svgwrite是一个 Python 库,用于生成简单的 SVG 图片。它提供了一组类似于绘图的 API,使用者可以在 SVG 画布上画线、矩形、圆等图形。本文主要介绍了如何利用svgwrite进行SVG图片的操作,需要的可以参考一下
    2023-01-01
  • Python Tkinter Menu控件使用详解

    Python Tkinter Menu控件使用详解

    Menu控件(菜单控件)可以说是 GUI 中“精髓所在”,它以可视化的方式将一系列的命令进行分组,在每一个分组下又可以“隐藏”许多的程序执行命令(即功能)。本文将详细介绍它的使用,需要的可以参考一下
    2022-01-01
  • Python实现简单登录验证

    Python实现简单登录验证

    这篇文章主要为大家详细介绍了Python实现简单登录验证的相关资料,需要的朋友可以参考下
    2016-04-04
  • pytorch实现查看当前学习率

    pytorch实现查看当前学习率

    这篇文章主要介绍了pytorch实现查看当前学习率,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • Python导入其他文件夹中函数的实现方法

    Python导入其他文件夹中函数的实现方法

    本文主要介绍了Python导入其他文件夹中函数的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01

最新评论