I'm trying to execute the following line of code:
subprocess.call(["java", "-cp", "/home/me/somepath/file.jar", ..., "-someflag somevalue"])
The code fails and the jar I'm trying to run gives me usage information. But if I expand out the string and paste it into the terminal, it works (I know I'm expanding the string out correctly because the sh module spits it back to me when it errors out). So this is an issue with how either subprocess or sh operates.
Here's an example of how you're supposed to use it:
subprocess.call(["ls", "-l"])
Here's the description:
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.
http://docs.python.org/2/library/subprocess.html
It's not clear to me if I should be breaking up the strings in the list with flags and values in separate places.
2 Answers 2
subprocess.call(["java", "-cp", "/home/me/somepath/file.jar", ..., "-someflag", "somevalue"])
Your original code corresponds to
java -cp /home/me/somepath/file.jar ... "-someflag somevalue"
in the shell.
Comments
toggle the shell flag to true
ie,
subprocess.check_call(["java", "-cp", cp_arg, ..., "-someflag somevalue"], shell=True)
also, a tip, you can use the split() function to split up a string command:
subprocess.check_call("java -cp blah blah".split(), shell=True)
1 Comment
shell=True.
"-someflag somevalue"up into"-someflag", "somevalue"and see if that helps.