Re: Calling Java jar class with parameter from Python

Discussion in 'Python' started by Peter Otten, Jul 21, 2012.

  1. Peter Otten

    Peter Otten Guest

    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()
    Peter Otten, Jul 21, 2012
    #1
    1. Advertising

  2. Peter Otten

    Roy Smith Guest

    In article <>,
    Peter Otten <> 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()
    Roy Smith, Jul 21, 2012
    #2
    1. Advertising

  3. Peter Otten

    Guest

    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.
    , Jul 21, 2012
    #3
  4. Peter Otten

    Guest

    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.
    , Jul 21, 2012
    #4
  5. Peter Otten

    Guest

    On Saturday, July 21, 2012 6:57:48 AM UTC-7, Roy Smith wrote:
    > In article &lt;&gt;,
    > Peter Otten &lt;&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'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.
    , Jul 21, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyril
    Replies:
    2
    Views:
    3,834
    cyril
    Aug 25, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    554
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,688
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,725
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    638
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page