Python基于pyjnius库实现访问java类
作者:Tracydzf 发布时间:2022-01-03 12:29:06
简介
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)
来源:https://www.cnblogs.com/tracydzf/p/13387900.html
猜你喜欢
- 本文实例讲述了Java实现指定线程执行顺序的三种方式。分享给大家供大家参考,具体如下:方法一:通过共享对象锁加上可见变量来实现。public
- Java定义Long数据类型Long lg=10L;只需要在定义的的整型后面加个L;就和定义float数据类型一样Float ft=5.20
- 若一个实例方法声明前带有virtual关键字,那么这个方法就是虚方法。虚方法与非虚方法的最大不同是,虚方法的实现可以由派生类所取代,这种取代
- 以上是集成测试后的Jprofiler演示效果图今晚想在IDEA中集成一下JProfiler11(现在有12版本了)工具,去网上看了下都是老版
- 背景在Java中一个回调的操作是一个在一些操作完成之后被传递到另一个函数中并且被执行的函数。一个回调函数既可以被同步或者异步执行。在一个同步
- 本文实例为大家分享了Java实现简单贪吃蛇游戏的具体代码,供大家参考,具体内容如下贪吃蛇小游戏制作方法首先需要的准备有:1、掌握Java基础
- 来源:https://www.cnblogs.com/fanweisheng/p/11440
- 0.解释器(Interpreter)模式定义 :给定一门语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中句子。
- 一、实验目的1. 掌握面向对象的编程思想、类与对象;2. 掌握类的封装性、继承性和多态性的作用;3. 掌握成员变量和成员方法的特性、构造方法
- java 实现随机数组输出及求和问题描述:随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息
- 今天老师想让我帮忙把她们200多张寸照换成白底的,这些寸照里面多为蓝色底,红色底。用ps?不!用java!!对,我第一反应就是用java,到
- Spring开启注解AOP的支持放置的位置放在springmvc的aop,需要在springmvc的配置文件中写开启aop,而不是sprin
- 出处:https://www.cnblogs.com/SunSpring作为初学者配置maven一般网上搜索。然后你就看到各种配置文件片段,
- 如下所示://定义二维数组写法1 class numthree{public static void main(String[] args)
- 整理文档,搜刮出一个java实现向有序数组中插入一个元素,稍微整理精简一下做下分享package cn.jbit.array; import
- MyBatis 获取子类的属性这里有个model类:基类public class user { pu
- 前言JAVA缓存实现方案有很多,最基本的自己使用Map去构建缓存,或者使用memcached或Redis,但是上述两种缓存框架都要搭建服务器
- 一、interrupt的使用特点我们先看2个线程打断的示例首先是可打断的情况:@Testpublic void interruptedTes
- 1、redis的几种常见客户端:Jedis:是Redis的Java实现客户端,提供了比较全面的Redis命令的支持;Redisson:实现了
- 问题今天在springboot中使用mybatis的时候不能字段不能够进行自动映射,mybatis的版本是3.5.11,数据库是按照下划线进