Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Calling Java jar class with parameter from Python

1,384 views
Skip to first unread message

Peter Otten

unread,
Jul 21, 2012, 8:20:48 AM7/21/12
to pytho...@python.org
Jason Veldicott wrote:

> 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?

File names with spaces can be tricky. Try thoroughly separating the
individual arguments and let subprocess do the necessary escaping.
I think it should be

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).communicate()


Roy Smith

unread,
Jul 21, 2012, 9:57:48 AM7/21/12
to
In article <mailman.2380.1342873...@python.org>,
Peter Otten <__pet...@web.de> wrote:

> 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).communicate()

You might also want to try raw strings. This should be identical to
Peter's version, but easier to read:

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

although I would probably refactor it like:

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

jasonve...@gmail.com

unread,
Jul 21, 2012, 12:03:57 PM7/21/12
to comp.lan...@googlegroups.com, pytho...@python.org
On Saturday, July 21, 2012 5:20:48 AM UTC-7, Peter Otten wrote:
> Jason Veldicott wrote:
>
> &gt; subprocess.Popen([&quot;C:\\Program Files
> &gt; (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe&quot;, &quot;-cp
> &gt; c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool&quot;,
> &gt; &quot;C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g&quot;],
> &gt; stdout=subprocess.PIPE, shell=True ).communicate()
> &gt;
> &gt;
> &gt; Obviously, some trick is being missed. Could anyone shed light on what it
> &gt; may be?
>
> File names with spaces can be tricky. Try thoroughly separating the
> individual arguments and let subprocess do the necessary escaping.
> I think it should be
>
> subprocess.Popen([
> &quot;C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe&quot;,
> &quot;-cp&quot;,
> &quot;C:\\antlr\\antlr-3.4-complete.jar&quot;,
> &quot;org.antlr.Tool&quot;,
> &quot;C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g&quot;],
> stdout=subprocess.PIPE).communicate()

That did the trick, thanks.

I had the impression from another post that the breaking up of command strings into subprocess arguments could be done arbitrarily as needed to deal with nested inverted commas. Obviously as you've shown, this is not the case, at least for Popen.

jasonve...@gmail.com

unread,
Jul 21, 2012, 12:03:57 PM7/21/12
to pytho...@python.org
On Saturday, July 21, 2012 5:20:48 AM UTC-7, Peter Otten wrote:
> Jason Veldicott wrote:
>
> &gt; subprocess.Popen([&quot;C:\\Program Files
> &gt; (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe&quot;, &quot;-cp
> &gt; c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool&quot;,
> &gt; &quot;C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g&quot;],
> &gt; stdout=subprocess.PIPE, shell=True ).communicate()
> &gt;
> &gt;
> &gt; Obviously, some trick is being missed. Could anyone shed light on what it
> &gt; may be?
>
> File names with spaces can be tricky. Try thoroughly separating the
> individual arguments and let subprocess do the necessary escaping.
> I think it should be
>
> subprocess.Popen([

jasonve...@gmail.com

unread,
Jul 21, 2012, 12:11:18 PM7/21/12
to
On Saturday, July 21, 2012 6:57:48 AM UTC-7, Roy Smith wrote:
> In article &lt;mailman.2380.1342873...@python.org&gt;,
> Peter Otten &lt;__pe...@web.de&gt; wrote:
>
> &gt; subprocess.Popen([
> &gt; &quot;C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe&quot;,
> &gt; &quot;-cp&quot;,
> &gt; &quot;C:\\antlr\\antlr-3.4-complete.jar&quot;,
> &gt; &quot;org.antlr.Tool&quot;,
> &gt; &quot;C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g&quot;],
> &gt; stdout=subprocess.PIPE).communicate()
>
> You might also want to try raw strings. This should be identical to
> Peter&#39;s version, but easier to read:
>
> subprocess.Popen([
> r&quot;C:\Program Files (x86)\Java\jdk1.7.0_05\bin\java.exe&quot;,
> r&quot;-cp&quot;,
> r&quot;C:\antlr\antlr-3.4-complete.jar&quot;,
> r&quot;org.antlr.Tool&quot;,
> r&quot;C:\Users\Jason\Documents\antlr\java grammar\Java.g&quot;],
> stdout=subprocess.PIPE).communicate()
>
> although I would probably refactor it like:
>
> args = [r&quot;C:\Program Files (x86)\Java\jdk1.7.0_05\bin\java.exe&quot;,
> r&quot;-cp&quot;,
> r&quot;C:\antlr\antlr-3.4-complete.jar&quot;,
> r&quot;org.antlr.Tool&quot;,
> r&quot;C:\Users\Jason\Documents\antlr\java grammar\Java.g&quot;,
> ]
> proc = subprocess.Popen(args, stdout=subprocess.PIPE)
> proc.communicate()

The r string notation at least saves having to double type a bunch of backslashes, although the appearance prepended to the string takes a little getting used to.

Visually the separate array to handle arguments is perhaps cleaner, having more resemblance to the original command.

Thanks for the tips.
0 new messages