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

标签:Python,pyjnius,java
0
投稿

猜你喜欢

  • c#文档图片自动纠偏

    2022-08-07 13:45:39
  • Eclipse设置断点调试的方法

    2022-11-05 07:45:56
  • Java给JFrame窗口设置热键的方法实现

    2022-01-24 13:32:20
  • spring boot的拦截器简单使用示例代码

    2021-09-29 04:07:07
  • C#中Linq的去重方式Distinct详解

    2022-01-23 03:53:36
  • Android 图片特效如何实现及总结

    2023-07-26 12:58:33
  • 详解C#获取特定进程CPU和内存使用率

    2022-06-23 03:06:42
  • Android Button按钮的四种点击事件

    2021-12-16 04:02:41
  • Java实现LeetCode(54.螺旋矩阵)

    2023-01-26 20:54:00
  • ReactNative Alert详解及实例代码

    2022-07-04 15:02:46
  • JAVA学习之一步步搭建spring框架

    2023-02-24 06:39:44
  • 在.net应用程序中运行其它EXE文件的方法

    2023-11-08 08:34:43
  • 如何基于JavaFX开发桌面程序

    2023-10-30 11:06:19
  • C#委托与事件初探

    2021-06-07 00:09:42
  • Java实现爬取百度图片的方法分析

    2023-12-19 23:51:27
  • C#中try...catch的使用与常见面试题分享

    2022-10-22 16:30:35
  • Mybatis中如何进行批量更新(updateBatch)

    2022-10-11 13:42:00
  • C#异步编程由浅入深(二)之Async/Await的使用

    2022-10-14 16:45:20
  • Android开发之activiti节点跳转

    2021-06-03 19:30:29
  • C#实现根据图片的链接地址获取图片的后缀名

    2022-08-04 04:30:58
  • asp之家 软件编程 m.aspxhome.com