0

Running this command in the terminal works fine:

java -jar file.jar --arg1 --arg2 pathTofile

When I then try to do this from the python code using following code:

subprocess.call(['java', '-jar', 'file.jar', '--arg1' , '--arg2' , 'pathTofile'])

I get an IllegalArugmentException for --arg1.

Any ideas on why this could generate a problem?

3
  • Can you modify or debug the Java program? Commented Oct 18, 2012 at 11:56
  • Linux and cannot debug the Java program, the java one works fine Commented Oct 18, 2012 at 12:05
  • the code in the question is fine. your answer indicates that the issue is that you put several command-line parameters into a single list item. The rule is simple: one parameter per item. Commented Oct 4, 2014 at 12:45

2 Answers 2

1

@Benst - I encountered the same problem, this is what works for me:

subprocess.call(['java', '-Dparam1=paramValue1', '-Dparam2=paramValue2', '-jar', 'filename.jar']);

Cheers!

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry but this is not the solution. I have This: ['java', '-jar', 'pdfbox-app-1.8.8.jar', 'ExtractText', 'Content.pdf', 'Content.pdf.txt'] and does not work. It gives Usage: java [-options] class [args...]. ...
@alemol - your usage is incorrect, look closely at the syntax...'-jar' and the 'filename.jar' should be at the end of the code...
Following the correct syntax: subprocess.call(['java', 'ExtractText', 'somepdf.pdf', 'sometxt.txt', '-jar', 'pdfbox-app-1.8.8.jar']) gives: Could not find or load main class ExtractText. So, How to call a module (with this usage) in python subprocess.call?: java -jar pdfbox-app-x.y.z.jar ExtractText [OPTIONS] <inputfile> [Text file]
0

Ok I figuered it out. When using the subprocess routine in python you need to quote the option and concurrent value seperatly

eg:

subprocess.call(['java', '-jar', 'file.jar', '-option', 'valueForThisOption', '-option', 'valueForThisOption', 'pathToFile'])

putting the -option and value in one quote generates this error. I have read somewhere that you can only put the -option and value in one quote if you put shell=True at the end. (untested)

1 Comment

Depending on the called program, you can write '--arg=itsParameter' in one argument.

Your Answer

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.