Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25742 > unrolled thread

Re: Calling Java jar class with parameter from Python

Started byPeter Otten <__peter__@web.de>
First post2012-07-21 14:20 +0200
Last post2012-07-21 09:03 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Calling Java jar class with parameter from Python Peter Otten <__peter__@web.de> - 2012-07-21 14:20 +0200
    Re: Calling Java jar class with parameter from Python Roy Smith <roy@panix.com> - 2012-07-21 09:57 -0400
      Re: Calling Java jar class with parameter from Python jasonveldicott@gmail.com - 2012-07-21 09:11 -0700
    Re: Calling Java jar class with parameter from Python jasonveldicott@gmail.com - 2012-07-21 09:03 -0700
    Re: Calling Java jar class with parameter from Python jasonveldicott@gmail.com - 2012-07-21 09:03 -0700

#25742 — Re: Calling Java jar class with parameter from Python

FromPeter Otten <__peter__@web.de>
Date2012-07-21 14:20 +0200
SubjectRe: Calling Java jar class with parameter from Python
Message-ID<mailman.2380.1342873263.4697.python-list@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()

[toc] | [next] | [standalone]


#25743

FromRoy Smith <roy@panix.com>
Date2012-07-21 09:57 -0400
Message-ID<roy-B96A3E.09574821072012@news.panix.com>
In reply to#25742
In article <mailman.2380.1342873263.4697.python-list@python.org>,
 Peter Otten <__peter__@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()

[toc] | [prev] | [next] | [standalone]


#25751

Fromjasonveldicott@gmail.com
Date2012-07-21 09:11 -0700
Message-ID<1d773886-2cd0-4f72-b061-1490db6567e8@googlegroups.com>
In reply to#25743
On Saturday, July 21, 2012 6:57:48 AM UTC-7, Roy Smith wrote:
> In article &lt;mailman.2380.1342873263.4697.python-list@python.org&gt;,
>  Peter Otten &lt;__peter__@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.

[toc] | [prev] | [next] | [standalone]


#25746

Fromjasonveldicott@gmail.com
Date2012-07-21 09:03 -0700
Message-ID<mailman.2386.1342886640.4697.python-list@python.org>
In reply to#25742
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.

[toc] | [prev] | [next] | [standalone]


#25747

Fromjasonveldicott@gmail.com
Date2012-07-21 09:03 -0700
Message-ID<f8b2335d-3beb-4b58-9fe1-9432a08dd457@googlegroups.com>
In reply to#25742
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web