Skip to content Skip to sidebar Skip to footer

Execute .jar From Python

I am trying to build a very simple python script to automate minifying/combining some css/js assets. I am not sure how to properly handle the minification step. I use yui-compresso

Solution 1:

I have a somewhat similar case, when I want a python program to build up some commands and then run them, with the output going to the user who fired off the script. The code I use is:

import subprocess
def run(cmd):
   call = ["/bin/bash", "-c", cmd]
   ret = subprocess.call(call, stdout=None, stderr=None)
   if ret > 0:
      print "Warning - result was %d" % ret

run("javac foo.java")
run("javac bar.java")

In my case, I want all commands to run error or not, which is why I don't have an exception raised on error. Also, I want any messages printed straight to the terminal, so I have stdout and stderr be None which causes them to not go to my python program. If your needs are slightly different for errors and messages, take a look at the http://docs.python.org/library/subprocess.html documentation for how to tweak what happens.

(I ask bash to run my command for me, so that I get my usual path, quoting etc)


Solution 2:

os.system should return 0 when the command executes correctly. 0 is the standard return code for success.

Does it print output when run from the command line?


Solution 3:

Why would you want to do this in Python? For tasks like this, especially Java, you are better off using Apache Ant. Write commands in xml and then ant runs for you.


Post a Comment for "Execute .jar From Python"