Q: Scripts to run Java programs - e.g. Ant 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:


*

*this is using Ant in a slightly unusual way, which is perhaps not how Ant was intended to be used (or at least, so it seems to me...)?

*I'm not really sure, but it seems that fewer and fewer developers/teams are using Ant these days, with preference for other technologies.


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



A: 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.

A: In a perfect world shutting down your Ant program would also shut down your Java programs.  It sounds like you need an explicit shutdown procedure.
Ant is great for writing platform-independent scripts.  I've never heard of a long-lived Ant process before.  In my experience the Ant program runs, does its job, and exits.  I would suggest setting spawn=true when you fire up the Java programs so they will continue after Ant dies, then allowing Ant to exit as quickly as possible.
You should make an explicit Ant task whose job it is to shut down the system.  The quick-and-dirty way is to find those Java programs and kill them.  Assuming these are programs that you wrote, you'll be able to incorporate explicit shutdown hooks.  For example, have each program listen on a predefined port and when someone connects to that port and issues the right instructions the program will initiate a graceful shutdown.
This suggestion comes with security risks -- what if a malicious program initiates the shutdown sequence?  You'll have to make sure your firewall is configured appropriately so that only trusted processes or machines can access the shutdown port.  That part is left as an exercise for the reader. :)
Edit: Make sure your Ant task and/or the Java program do appropriate error checking on any parameters that are passed in.  Otherwise you could have a situation that is really challenging to debug. (Thanks to @randomA)

A: Two options that I used in my last project:


*

*maven assembly plugin: this plugin can generate for you a "package" that contains the libraries you need and a generated shell script to execute the program. Another option I like a lot is to generate a "fat jar" with all the libs merged in a big .jar. This simplifies a lot the distribution and executing of java programs and you can simple execute with java <class_name> -jar <ftr-jar.jar>

*Use groovy: I like a lot this option because gives me a lot of freedom. You can have .groovy script that simply calls your classes. Inside this scripts you can use "grab" to declare your dependencies (more info in this link: http://groovy.codehaus.org/Grapes+and+grab()).  

A: Since you are already using Maven, you may consider giving the Exec Plugin a shot.
I would rather avoid custom runs scripts if I were you unless the startup and shutdown logic is really complex, since writing them properly may be much harder than it seems, and you can run into issues with cross-platform or even cross-machine compatibility.
If you need lots of custom logic in the build or run process but prefer to stay within the bounds of a build system, you might want to have a look at Gradle which allows you more customization than Maven in some cases since you can add almost any Groovy code into your build process. This approach allows for better integration and may be more portable than writing custom scripts independent of the build system.

A: Most of the larger java tooling like Maven and Ant have batch/shell script wrappers.
So if you want to be cross platform you need to have both.  http://www.mojohaus.org/appassembler/appassembler-maven-plugin/assemble-mojo.html tries to automate the creation of those scripts for you for both Windows and Unix.
