栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python工具库介绍-pyjnius:访问java类

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python工具库介绍-pyjnius:访问java类

安装

先安装Java JDK 和JRE、Cython

# pip3 install cython# pip3 install jniusxCollecting jniusx

  Downloading jniusx-1.0.5.tar.gz

Requirement already satisfied: six>=1.7.0 in /opt/python3.5/lib/python3.5/site-packages (from jniusx)

Requirement already satisfied: cython in /opt/python3.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# ipythonPython 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作业AI毕业设计
链接:https://www.jianshu.com/p/5c7c3e514e37


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/221350.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号