DOS batch files to compile and run a Java program (and create a jar file)

This isn't the most high-tech way to do things, but I thought I'd share these Windows (DOS) shell scripts that I'm currently using to compile a Java application, create a Jar file to distribute the application, and finally run the application. I ended up creating these scripts because of configuration problems on my Windows PC, but I thought they might be useful samples for others.

As you'll see the mostly correct problems with my JAVA_HOME, PATH, and CLASSPATH configurations. I should use an Ant build script for this, but I'm too lazy today. I'm also overriding my Java compiler settings on this PC, as I have several Java versions installed on this system.

Here's the first script I named 1compile.bat that I use to compile my Java program:

set JAVA_HOME=C:\jdk1.5.0_06
set PATH=C:\Windows;C:\Windows\System32;C:\jdk1.5.0_06\bin
set CLASSPATH=

javac -cp ".;forms-1.0.7.jar" com/devdaily/xylocator/XYLocator.java

Here's the second script I named 2createJar.bat that I use to create a jar file that I distribute with my Java program:

set JAVA_HOME=C:\jdk1.5.0_06
set PATH=C:\Windows;C:\Windows\System32;C:\jdk1.5.0_06\bin
set CLASSPATH=

jar cfm xylocator.jar Manifest.txt com/devdaily/xylocator/*.class

That command also requires a file named Manifest.txt, which looks like this:

Main-Class: com.devdaily.xylocator.XYLocator
Class-Path: forms-1.0.7.jar

The first line in the manifest file tells the JVM where to find the main method to run my program, and the second line helps configure the CLASSPATH, which is required because this is a Java Swing application that uses the JGoodies FormLayout.

That script creates a jar file (with the manifest included) so I can just use the java -jar command to run my program. I include that command in a batch file I named 3runJar.bat that looks like this:

set JAVA_HOME=C:\jdk1.5.0_06
set PATH=C:\jdk1.5.0_06\bin;C:\Windows;C:\Windows\System32
set CLASSPATH=

java -jar xylocator.jar

FWIW, I'm actually very lazy, as you can tell from the file names I've shown. I use the 1, 2, and 3 as filename prefixes to remind me the order things should be run in later. That's pretty lazy. :)