Java – Scripts to run Java programs – e.g. Ant

antjava

My team currently use an Ant script to execute various Java programs.

Note that I am not asking about managing the build/deployment cycle for which we are already using Maven (quite happily).

For example, we use something like the following:

<path id="lib">
  <fileset dir="path/to/lib" />
</path>

<path id="run" >
  <path refid="lib" />
</path>

<target name="aJavaProgram" >
  <java classname="path.to.AJavaProgam" 
        classpathref="run" fork="yes" failonerror="false" append="true" />
</target>

<target name="anotherJavaProgram" depends="aJavaProgram" >
  <java classname="path.to.AnotherJavaProgam" 
        classpathref="run" fork="yes" failonerror="false" append="true" />
</target>

We originally chose Ant for the following reasons (or rather I didn't choose Ant, it was that way when I joined, but I think these are good reasons for the choice):

  • it was an easy way of managing dependencies (although we now pick up these from Maven)
  • it allows us to easily build/maintain dependencies between particular jobs
  • we can deploy the Ant script, and the Java libraries, and easily run particular jobs – i.e.
    • we don't have to worry about classpaths (configured in the Ant script)
    • we don't have to worry about building jars with main classes specified in the manifest
    • we can use Ant to create / store / manage arguments passed to main() methods
  • we can plug these ant scripts into our IDEs and run the jobs from there.

Generally speaking, we have been very happy with this.

However, an increasing issue is the ant-java-shutdown hook issue – this means that if one of these Java programs fails but doesn't finish, terminating the Ant process from which it was started doesn't terminate the Java process – which then has to be done manually, and is a real bind.

Also, I'm conscious of two other (possible) factors:

So, to clarify:

  • I want a flexible approach to execute various Java programs
  • the approach that we have been using was to do this via Ant scripts, but this had the following problems:
    • terminating the Ant task does not kill the Java program
    • this "feels" like Ant is not the right tool for the job
  • how might we meet the needs specified above?
    • if not Ant, the solution should not face the termination / shutdown hook issue

Best Answer

It sound like you are running some Java programs in a sequence and basically you were using Ant to call java as java -jar myApplication.jar ...

Normally, I see this done using shell scripts. They are simple, small, and very good for redirecting output to files, other programs, etc...

If you want something that is more cross-platform compatible, you could look into using a scripting language such as Perl or Ruby or Python, they all allow you to call java -jar myApplication.jar and it could be as simple as 1 or 2 lines to run your Java program. There are versions of all of these that are available for Windows, OS X, Linux and Unix. And you get the full flexibility of these languages, should you actually need them.

Additionally, most IDEs support launching "external"/"3rd party" tools from within the IDE, so you can write a shell (or Perl or whatever) script to launch your Java programs, and then in your IDE, it should be possible to create a link/button/whatever to launch myJavaAppRunner.ksh at the click of a button.