Execute Java programs in a consistent environment Execute Java programs in a consistent environment shell shell

Execute Java programs in a consistent environment


Have you considered generating the shell scripts from Ant? The <pathconvert> task can create a classpath from a <fileset>.

You can create an Ant target for each kind of shell script you want. make can then call these Ant targets to generate the shell scripts.


I think the solution to your problem is to create an executable jar. Something that can be run as follows:

java -jar myapp.jar

"look ma, no classpath" :-)

The secret is add the "Main-Class" and "Class-Path" entries into the jar's manifest file. This tells java what to run and which jars should be loaded onto the classpath:

<jar destfile="${jar.file}" basedir="${classes.dir}">    <manifest>        <attribute name="Main-Class" value="${jar.main.class}" />        <attribute name="Class-Path" value="${jar.classpath}" />    </manifest></jar>

To assist in creating the classpath, ANT has a really useful manifestclasspath task:

<manifestclasspath property="jar.classpath" jarfile="${jar.file}">    <classpath>        <fileset dir="${dist.dir}/lib" includes="*.jar"/>    </classpath></manifestclasspath>

So using this example, at run-time java will expect the depedencies to reside in a "lib" subdirectory (the task will generate relative links).

Eclipse integration is tricky. A better approach for managing your classpath is to use a dependency manager like ivy, which has an Eclipse plugin. In this way both ANT and Eclipse use the same mechanism for controlling build dependencies.

Finally for a complete working example take a look at:

Hope this helps.


With Google's Bazel build system, you can easily define as many runnable Java programs as you want, with the java_binary target. You can define as many java_binary targets as you'd like, and they can all depend on the same set (or overlapping sets) of java_library targets so you don't need to manually curate the classpath of each binary.

Additionally, with a java_binary target you can also generate a *_deploy.jar file, which is a standalone executable Jar you can run anywhere.