This isn’t the most high-tech way to do things, but I thought I’d share these Windows (DOS) shell scripts that Batch file

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:WindowsSystem32;C:jdk1.5.0_06bin
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:WindowsSystem32;C:jdk1.5.0_06bin
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_06bin;C:Windows;C:WindowsSystem32
set CLASSPATH=

java -jar xylocator.jar

 

How to create a cmd file to run jar tutorial

 

 

One thought on “How to create batch files for Java program tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *