How to get the output from .jar execution in python codes?(如何在 python 代码中获取 .jar 执行的输出?)
问题描述
我正在编写 python 模块,该模块执行 SQL 到 DBMS 并检索数据.我正在尝试使用 jdbc jar 文件而不是本机 DB 驱动程序.我想知道如何在 python 中执行 jar 文件并从 jar 执行中获取输出.而且我想知道如何将 SQL 字符串传递给 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代码]
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代码]
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
这篇关于如何在 python 代码中获取 .jar 执行的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 python 代码中获取 .jar 执行的输出?
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01