1

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.

asked Nov 29, 2012 at 3:12
3
  • What error are you getting? Commented Nov 29, 2012 at 3:14
  • I'm getting help from the program on the proper way to specify arguments. Commented Nov 29, 2012 at 3:17
  • Break "-someflag somevalue" up into "-someflag", "somevalue" and see if that helps. Commented Nov 29, 2012 at 3:18

2 Answers 2

3
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.

answered Nov 29, 2012 at 3:18
Sign up to request clarification or add additional context in comments.

Comments

0

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)

answered Nov 29, 2012 at 3:14

1 Comment

I don't think you need to split up the command if you're using shell=True.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.