简体   繁体   English

如何使用Python的子进程和Popen执行带有插值参数的Java命令

[英]How to execute a java command with interpolated arguments with Python's subprocess & Popen

trying to run a java command from within python on Windows, using subprocess & Popen. 尝试使用子进程和Popen在Windows上的python中运行Java命令。

First I've defined two strings: 首先,我定义了两个字符串:

java_string = 'java -Xmx1024M -classpath .;./db2jcc.jar;./RetainSdi.zip;./jviews-chart.jar;./jviews-framework.jar l2stats CRF1426 > ' + datetime.date.today().strftime("%d/%m/%Y") + '.html'

working_dir = "C:\\Users\\IBM_ADMIN\\Desktop\\Weekly Report 2014\\"

then I tried to use subprocess as follows: 然后我尝试使用子流程,如下所示:

subprocess.Open([java_string], cwd=working_dir)

which results in a "FileNotFoundError: [WinError 2] The system cannot find the file specified" 结果为“ FileNotFoundError:[WinError 2]系统找不到指定的文件”

I've edited the long java_string to not have the java command, like so: 我编辑了很长的java_string使其没有java命令,如下所示:

java_string = '-Xmx1024M -classpath .;./db2jcc.jar;./RetainSdi.zip;./jviews-chart.jar;./jviews-framework.jar l2stats CRF1426 > ' + datetime.date.today().strftime("%d/%m/%Y") + '.html'

and then called Popen like so: 然后像这样调用Popen:

subprocess.Popen(['java', java_string], cwd=working_dir)

which results in a 导致

Invalid maximum heap size: -Xmx1024M -classpath .;./db2jcc.jar;./RetainSdi.z ip;./jviews-chart.jar;./jviews-framework.jar l2stats CRF1426 > 21/04/2014.html Error: Could not create the Java Virtual Machine. 无效的最大堆大小:-Xmx1024M -classpath。; ../ db2jcc.jar; ./ RetainSdi.z ip; ./ jviews-chart.jar; ./ jviews-framework.jar l2stats CRF1426> 21/04 / 2014.html错误: 无法创建Java虚拟机。

which is at least a java error (so it's finding the java exec. in the %PATH%, but has to do with java seeing the entire string as a single argument? I have no idea. 这至少是一个Java错误(因此它在%PATH%中找到了Java exec,但是与Java将整个字符串视为单个参数有关?我不知道。

What is the correct formatting here? 此处的正确格式是什么? Any help much appreciated! 任何帮助,不胜感激!

You probably need to split the arguments into a list, as described in the documentation : 您可能需要将参数拆分为一个列表,如文档中所述:

Unless otherwise stated, it is recommended to pass args as a sequence. 除非另有说明,否则建议将args作为序列传递。

So instead of: 所以代替:

subprocess.Popen(['java', '-Xmx1024M -classpath . com.foo.Bar']
# Error (Invalid maximum heap size)

use: 采用:

subprocess.Popen(['java', '-Xmx1024M', '-classpath', '.', 'com.foo.Bar']
# OK

See also the Windows-specific rules for converting an argument sequence . 另请参阅Windows特定的用于转换参数序列的规则

Updated : If you need to use features of the shell, such as redirection to a file using > , then you need to include the shell=True argument, for example: 已更新 :如果您需要使用shell的功能,例如使用>重定向到文件,则需要包含shell=True参数,例如:

subprocess.Popen('echo foo > deleteme.txt', shell=True)

Or specify a stdout argument as described in the comment from JF Sebastian (copied here for convenience): 或按照JF Sebastian的注释中所述指定stdout参数(为方便起见,在此复制):

subprocess.call(['echo', 'foo'], stdout=open('deleteme.txt', 'wb'))

One note from the documentation: 文档中的一个注释:

Passing shell=True can be a security hazard if combined with untrusted input. 如果与不受信任的输入结合使用,则传递shell = True可能会带来安全隐患。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM