Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Calling Java jar class with parameter from Python

Reply
Thread Tools

Re: Calling Java jar class with parameter from Python

 
 
Peter Otten
Guest
Posts: n/a
 
      07-21-2012
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()


 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      07-21-2012
In article <(E-Mail Removed)>,
Peter Otten <(E-Mail Removed)> 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()
 
Reply With Quote
 
 
 
 
jasonveldicott@gmail.com
Guest
Posts: n/a
 
      07-21-2012
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.
 
Reply With Quote
 
jasonveldicott@gmail.com
Guest
Posts: n/a
 
      07-21-2012
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.
 
Reply With Quote
 
jasonveldicott@gmail.com
Guest
Posts: n/a
 
      07-21-2012
On Saturday, July 21, 2012 6:57:48 AM UTC-7, Roy Smith wrote:
> In article &lt;(E-Mail Removed)&gt;,
> Peter Otten &lt;(E-Mail Removed)&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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java -cp a.jar -jar b.jar => Works on Windows, not on Debian cyberco Java 4 02-14-2006 06:27 AM
jaas.jar, jta.jar jdbc-stdext.jar missing from jdk1.5 RPM muttley Java 0 10-20-2005 02:40 PM
Differences of xercesImpl.jar, xercesImpl-J.jar, dom3-xercesImpl.jar ? Arnold Peters Java 0 01-05-2005 10:59 PM
Differences of xercesImpl.jar, xercesImpl-J.jar, dom3-xercesImpl.jar ? Arnold Peters XML 0 01-05-2005 10:59 PM
instanciate a class in a jar file with class.forname, while my main class is in another jar cyril Java 2 08-25-2004 06:55 AM



Advertisments