python调用jar传参,如何从python代码中获取.jar执行的输出?

I'm programming the python module that executes SQL to DBMS and retrieves data. I'm trying to use jdbc jar files instead of native DB drivers. I'm wondering how to executes jar file in python and get output from jar execution. And I'd like to know how to pass SQL string to jar argument.

Here is the simplified code.

Any help is greatly appreciated.

[ java code ]

public class GetDBResults {

public static void main(String[] args) {

// return sql results

for(int i=0; i<=100; i++){

// Is this the proper way to generate the output?

System.out.println(i+"/t"+i*100+1);

}

}

}

[ python code ]

subprocess.call( [ 'java','-jar','./GET_DB_DATA.jar' )

# how to get results from jar execution?

# how to pass SQL string to jar execution?

解决方案

You can read the output through pipe:

>>> from subprocess import Popen, PIPE, STDOUT

>>> p = Popen(['java', '-jar', './GET_DB_DATA.jar'], stdout=PIPE, stderr=STDOUT)

>>> for line in p.stdout:

print line

As regards passing string to stdin, you can achieve it this way:

>>> p = Popen(['cat'], stdin=PIPE, stdout=PIPE, stderr=STDOUT)

>>> stdout, stderr = p.communicate(input='passed_string')

>>> print stdout

passed_string

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值