Calling Java jar class with parameter from Python

Jason Veldicott jasonveldicott at
Sat Jul 21 13:35:41 CEST 2012


I have read a number of posts on how this can be done, but I have not been
able to replicate success with the particular command I'm wishing to

I am wanting to execute the following Java command from Python in Windows:

java -cp c:\antlr\antlr-3.4-complete.jar org.antlr.Tool
"C:\Users\Jason\Documents\antlr\java grammar\Java.g"

This command works outside of Python at the command prompt.

So I do the same using Python's os.system:

os.system("C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe -cp
c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool
'C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g'")

The return value is 1, which presumably indicates error.

Another variation using, one of many tried, gives the same
result:["C:\\Program Files
(x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp
c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool",
"C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"]  )

This variation using subprocess.Popen, gives a result, but it's ('', None).

subprocess.Popen(["C:\\Program Files
(x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp
c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool",
"C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"],
stdout=subprocess.PIPE, shell=True ).communicate()

Obviously, some trick is being missed.  Could anyone shed light on what it
may be?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list