在python中引用jar包
在本文中,首先创建一个maven项目,然后打包成jar包,在python中调用jar包,执行java类方法。
1.在eclipse中创建maven项目
我这里创建一个普通java项目,择选择 “maven-archtype-quickstart” 即可
2.创建后,在项目根目录下会有一个pom.xml
3.编写pom文件,加载项目所需要的依赖,我的pom.xml 如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rfchina</groupId> <artifactId>utils</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>utils</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency> </dependencies> <!-- maven打包构建时,使用参数 assembly:assembly 则会加载依赖包 --> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2</version> <configuration> <archive> <manifest> <!-- 我运行这个jar所运行的主类 --> <mainClass>com.rfchina.utils.RsaSign</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef> <!-- 必须是这样写 --> jar-with-dependencies </descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> </project>
4.编写java代码前,这里我们先安装一下依赖包:
pom.xml 右键->Run As ->Maven Install
运行后,可以在Maven Dependencies中看到已经下载并且引入的maven依赖包
5.开始编写java代码
Base64Utils.java
package com.rfchina.utils; import org.apache.commons.codec.binary.Base64; public class Base64Utils { /** * 使用Base64加密字符串 * @return 加密之后的字符串 * @exception Exception */ public static String encode(byte[] data){ Base64 base64 = new Base64(); String encodedData = base64.encodeAsString(data); return encodedData; } /** * 使用Base64解密 * @return 解密之后的字符串 * @exception Exception */ public static byte[] decode(String data){ Base64 base64 = new Base64(); byte[] decodedData = base64.decodeBase64(data); return decodedData; } }
RsaSign.java
package com.rfchina.utils; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.Signature; import java.security.spec.PKCS8EncodedKeySpec; /** * @ClassName: RsaSign * @Description: RSA 私钥签名 * @author Milton * @date 2018年9月4日 * */ public class RsaSign { private static final String SIGNATURE_ALGORITHM = "SHA256withRSA"; private static final String KEYPAIR_GENERATOR_ALGORITHM = "RSA"; // 商户私钥加密接口请求参数 final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMJSignj27blOez6i6YCM1b4AC9CtgubD0B9gmTskENJNxzg9i59hCpIibkTg1rvMDdSuKFvlHtxkOgSID8Qgm1h3AxpMKpYT58FnApqY7img85xEZo+uNB1GHTbrhuCGQFf2P8U1hE01Y6miFsESsFTQ09BpNy+1wPKt/KbW75fAgMBAAECgYA/04iPkw4Z1tTd57Vyw4pFaJP28fyFd1rdHdx0ddc0opm9nI5/2q5MjSLfbW9ZsPKvWTZXoCSvHzAvabS5whx0aYpZlfWCd6QRNAuoaP0FSCWH/ty7I3nHQJK8LQQhP3nfekAfiyMpvGGK4KrderEP37/MG1202iycR4X6fZnMkQJBAPqaKChFYAqStADg3owoux5Gc3rAf2zRXIBZXPNEYgE0owpwtP2tYPdcIy9l01Yv1nIDr2O7x8JrbUOuNe/4/4sCQQDGggz4XHqZAwDBvea3g9FBCnojzyqQMtHO54TxL6NXIr7maBoBb0XCXuh6Uz2F7O8an1Bi/adQXVXUhvErG9b9AkBJhU6AuhG6KF4M3+wKnKyA7lRU0ALSTv3fXdhKOmaySdoHZxeCUQpgp7Re5HXDFFfKrVAYZ2/slw3ATGzgkWGPAkBW5b1px4n/i3n8VfY2paSntT9sh5bZUvXXfjALKNB3J4Wr9SxVLnG6ObPJQMEw7FxrKgyVmPZyTrlw9LWEKoa9AkBz9OU/BFhg9wIcHiFWQSOQdKQ4touyTF3T3EbROt34oXEhp1b3/eEGlwvNF2dUrfi39b5rKph63G6d3rxb+GVG"; public String rsa_sign(String encryData) { // String encryData_example="app_id=12996762&app_secret=902ac71d8fdbdfc097f53070afbcaefb&platform=1&sign_type=RSA×tamp=1536032317"; String sign = ""; try { byte[] buffer = Base64Utils.decode(privateKey); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance(KEYPAIR_GENERATOR_ALGORITHM); PrivateKey prK = keyFactory.generatePrivate(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(prK); signature.update(encryData.getBytes()); sign = Base64Utils.encode(signature.sign()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return sign; } public static void main(String[] args) { // TODO Auto-generated method stub RsaSign rs = new RsaSign(); String encryData = "app_id=12996762&app_secret=902ac71d8fdbdfc097f53070afbcaefb&platform=1&sign_type=RSA×tamp=1536032317"; System.out.println(rs.rsa_sign(encryData)); } }
其中RsaSign是我主要想要调用的工具类。
6.打包即可,这里注意,打包时需要将maven依赖包“commons-code-1.11.jar”也打进来,顾需要通过插件“maven-assembly-plugin”,前面已经加入在pom中了。
pom.xml-》右键-》Run AS -》Maven Build...-》指定Goals参数:assembly:assembly -》Run
7.打包成功后,在target目录下可以得到需要的jar包,如
8.将jar包放到python项目下,下面我们开始编写python代码,进行jar包的引用
jar_utils.py
import jpype def get_rsa_sign(sign_raw): """ 调用java jar包,对入参进行rsa签名 :param sign_raw:待签名字符串 :return:signature:签名后的加密字符串 """ # 启动JVM jvmPath = jpype.getDefaultJVMPath() # 加载jar包 jpype.startJVM(jvmPath, "-ea", "-Djava.class.path=utils-0.0.1-SNAPSHOT-jar-with-dependencies.jar") # 指定main class JDClass = jpype.JClass("com.rfchina.utils.RsaSign") # 创建类实例对象 jd = JDClass() # 引用jar包类中的方法 rsa_sign signature = jd.rsa_sign(sign_raw) # 关闭JVM jpype.shutdownJVM() return signature if __name__ == '__main__': sign_raw = "app_id=68613717&app_secret=039b8b6f1544f62dace6fe53e4c380be&platform=1&sign_type=RSA×tamp=1536039464" print("rsa 签名结果:",get_rsa_sign(sign_raw))
运行结果如下:
JVM has been shutdown
rsa 签名结果: Z8OqE9ldgp7+gG6+fFm/N17pqQAIvGzQY7VztYzWgsSEYiyi7g9piHPIZqB22ekRZfzPFpOXM0lXc+GDjZWYwN+0XJGvJYQMWDGuTgHcf8c6VhiiOgUHXtptHG4Op+06v3SJ1iNaEy8F5tH9Y74X6GhTqR28SHuW3sdep1AhExY=