1. 程式人生 > >Python2.7呼叫jar包方法例項

Python2.7呼叫jar包方法例項

請尊重版權,轉載請註明出處。謝謝!

jar包所依賴的檔案和C:\Program Files\Java\jre1.8.0_191\lib\ext檔案目錄下的jar包要全部一起放到同個資料夾(如:C:/lo4jar),否者會提示找不到類。

#!/usr/bin/env python
#coding:utf-8
import requests
import unittest
import json
import mmap
import jpype
from jpype import *
import os.path


def test_login():
    url = 'http:xxxxxxxxxxxxxxxx'
    jarpath2 = r'com.foresee.open.sdk.client-2.1.8-jar-with-dependencies.jar'
    jarpath3 = r'foresee-util.jar'
    jarpath9=os.path.join(os.path.abspath('.'), 'C:/lo4jar')# jar所依賴的檔案全部丟入一個資料夾
    jvmPath = jpype.getDefaultJVMPath()
    print(jvmPath)
    jpype.startJVM(jvmPath, "-ea", "-Djava.class.path=%s;%s" %(jarpath2,jarpath3),"-Djava.ext.dirs=%s"%jarpath9)
    tclass1 = jpype.JClass("com.foresee.util.TranSeqGeneratorNumUtil")

    tclass = jpype.JClass("com.foresee.open.sdk.client.OpenApiClient")

    TranSeq = tclass1.generateTranseq("frse", "", "")  # 靜態方法
    print("流水號:")
    print(TranSeq)

    customerAppId ="10002017"
    providerAppId="10001101"
    key="W9y4DjA5qF90A7Kg"
    t1 = JPackage("com.foresee.open.sdk.client").OpenApiClient(customerAppId , key)  # 動態方法
    concent2 = "{\"sjly\": \"\", \"yhzh\": \"PYuansan\", \"yhmm\": \"wmHux8j273I=\", \"sjc\": \"\", \"dljklx\": \"smz\"}"
    body = {

        "tranSeq": "" + TranSeq + "",
        "areaCode": "44",
        "nsrsbh": "91441K",
        "contentEncryptType": "",
        "gdslxDm": "1",
        "appid": "",
        "clientNo": "",
        "djxh": "",
        "sessionid": "",
        "contentType": "JSON",
        "content": "" + concent2 + "",
        "swjgDm": "00000000000",
        "sjjg": "00000000000"
    }
    bodyjson=json.dumps(body)
    print("請求報文是:")
    print(bodyjson)
    rps=t1.requestApp(url,bodyjson,providerAppId)
    print("返回資訊是:")
    print(rps)
    jpype.shutdownJVM()

if __name__ == '__main__':
     test_login()