yyxin  

加载jar包,其中os.pathsep.join表示将字符串使用;号相隔,一同加载到-Djava.class.path路径,即可以同时调用加载的所有jar包中的类

classpath = os.pathsep.join((
       r"D:\code\jar\jar\finsettsJsf-service-api-0.0.1-SNAPSHOT.jar",
       r"D:\code\jar\jar\jsf-1.6.0.jar",
       r"D:\code\jar\jar\finsettsJsf-domain-0.0.1-SNAPSHOT.jar"))

# 当指定了jar包后,需要将路径添加到-Djava.class.path路径下,使用addClassPath方法时则不用
startJVM(getDefaultJVMPath(),"-ea", "-Djava.class.path=%s" % classpath)

 

添加目录下的所有jar
jpype.addClassPath("jar/*")

 

引入java中的包
import jpype.imports

# 在java中显示为
import com.jd.jsf.gd.config.ConsumerConfig;
import com.jd.jsf.gd.config.RegistryConfig;

 

开启JVM服务
# 可以手动加载jvm.dll
jvm_path = 'C:\Program Files\Java\jre1.8.0_211\bin\server\jvm.dll'
startJVM(jvm_path,"-ea")

# 或直接通过方法获取jvm路径
startJVM(getDefaultJVMPath(),"-ea")

 

创建对象
# JClass是创建对象的方法
jsfRegistry = JClass("com.jd.jsf.gd.config.RegistryConfig")
jsfRegistry.setIndex("i.jsf.jd.com")

# 等价于Java中创建对象的方法
RegistryConfig jsfRegistry = new RegistryConfig();
jsfRegistry.setIndex("i.jsf.jd.com")

 

应用实例
from jpype import *

# 如果没有使用此行代码,那么在开启JVM后,无法使用from com.jd.jsf.gd.config import RegistryConfig导入jar包中的类
import jpype.imports

def doAutosett():

   # 通过指定路径来查找路径下的所有jar包
   jpype.addClassPath("jar/*")

   # 启动虚拟机,用-ea可打开断言机制,不加<packagename>和classname时运行所有包和类中的断言,如果希望只运行某些包或类中的断言,可将包名或类名加到-ea之后。
   startJVM(getDefaultJVMPath(),"-ea")

   # 导入jar包
   from com.jd.jsf.gd.config import RegistryConfig
   from com.jd.finsetts.service import FinSettGeneralApi
   from com.jd.jsf.gd.config import ConsumerConfig
   from com.alibaba.fastjson import JSON
   from java.util import LinkedList
   from com.jd.finsetts.pojo import FinFeeDetailQuery
   from com.jd.finsetts.export.model.result import Result

   # 创建对象,等价于RegistryConfig jsfRegistry = new RegistryConfig();
   jsfRegistry = RegistryConfig()
   # 调用对象中的方法
   jsfRegistry.setIndex("i.jsf.jd.com")

   consumerConfig = ConsumerConfig()
   consumerConfig.setInterfaceId("com.jd.finsetts.service.FinSettGeneralApi")
   consumerConfig.setAlias("finSettGeneralApi")
   consumerConfig.setProtocol("jsf")
   consumerConfig.setRegistry(jsfRegistry)
   consumerConfig.setProtocol("jsf")
   consumerConfig.setRegistry(jsfRegistry)

   # 字符串分别使用'''、""和',结构为列表中第一个为json对象,第二个是一个单引号的字符串,在单引号的字符串中包含3个json对象,那么第二个对象就是一个包含3个json对象的字符串
   autoSettString = '''
  [{"amount_GE":"","amount_LE":"","bizBType":"0","bizId":"test_0509360043279238_01267958111601_h","bizType":"532","condType":"1","creator":"lianghuaibin","customerId":"","customerName":"","direction":"","editor":"lianghuaibin","exOne":"","exThree":"","exTwo":"","feeId":"","feeType":"53202","feeTypes":"53202","firstSettle":false,"ids":"","orgId":"bjpa4578","payChannelType":"","productId":"","productName":"","productSku":"","remark":"","settApplyType":"1","sourceId":"","status":"","templateType":"103A"},'{"finSett": {"amount": 16.36,"bizType": "532","customerId": "1039269", "customerName": "测试", "customerType": "2","direction": "1","extend": "","feeType": "53202", "feeTypes": "53202","principalCompany": "10000041","productId": "aggreProfit","productIdFather": "","productName": "测试","remark": "1232","settAmount": 16.36,"settApplyType": "1","settPercent": "100","settType": "WIRE"},"finSettPayInfo": {"amount": 16.36,"bankAccount": "2343657561232423432432","bankBranch": "北京支行","bankCombNumber":"234324","bankName":"聚合测试","bankNo":"ABC","cardid": "","companyName":"宿迁际扬信息科技有限公司","othBankAbbreviation":"bjpa4578","othBankAccount":"2222555443324","othBankBranch":"平安银行股份有限公司北京丰台支行","othBankCombNumber": "307100003264","othBankName":"平安银行"},"settCount":0}']
  '''
   
   # LinkedList<Object> list = JSON.parseObject(autoSettString, LinkedList.class);
   # java语句表现为如上,表示创建LinkedList类型的list变量,其值为...
   # 无需加上之前的类型,直接为list赋值即可
   list = JSON.parseObject(autoSettString,LinkedList.class_)

   # 当传入的参数为int类型时,python需要转换为java的数据类型,使用JInt(0)传入
   pageQuery = JSON.parseObject(list.get(JInt(0)).toString(), FinFeeDetailQuery.class_)
   list.remove(JInt(0))
   list.add(JInt(0), pageQuery)
   service = consumerConfig.refer()

   # result = Result()
   result = service.autoSett(list)
   print(result.toString())
   return result.toString()

if __name__ == '__main__':
   doAutosett()

 

最后附上JPype的官方文档:

https://jpype.readthedocs.io/en/latest/index.html

 

posted on 2019-08-01 17:50  yyxin  阅读(596)  评论(0)    收藏  举报