Dynamically build a Java classpath in a Windows batch file

I don't work in the DOS (Windows command (CMD.EXE)) world too much these days, but I needed to today. The basic problem was that I needed to build an environment variable dynamically. In this case I was trying to build a variable named CLASSPATH from a collection of Java JAR files in a sibling directory named LIB. This variable had to be built correctly for my Java application to work properly.

I couldn't get a lot of obvious approaches to work, but then remembered I had seen a trick back in the release of Jakarta Tomcat 3.3.x. Sure enough it was in there, and I'd like to share it here.

To accomplish the desired effect you need two scripts. The first script is the one you want to build. In this case my script is named runit.bat. It has a "for" loop, and in that for loop I call the second script (CPAPPEND.BAT), like this:

REM runit.bat (by Alvin J. Alexander)
REM
REM SET THE CLASSPATH DYNAMICALLY
REM
REM Call the CPAPPEND.BAT program to build the JARS env variable,
REM then append that variable to the CLASSPATH.
REM To be clear, CPAPPEND.BAT builds the JARS variable.
REM (Note: Borrowed this approach from the 
REM Jakarta Tomcat people - thanks!)
REM

set JARS=
set CLASSPATH=
for %%i in (..\lib\*.jar) do call cpappend.bat %%i
set CLASSPATH=%JARS%;..\dist\myApplication.jar

java com.devdaily.myapp.MainController

Hopefully that batch file (I want to call it a script, but this isn't Unix land) makes sense. I'm building the variable CLASSPATH dynamically from a list of "*.jar" files that are located in a sibling directory named "lib". This CLASSPATH variable is needed by my Java application.

I wanted this approach because I hate to do things like this statically. If I did it with a static approach, I'd have to modify this script every time I added a new JAR file, and I sure don't like that approach. DOS is a little weak in this area, but the approach of using a separate batch file (CPAPPEND.BAT) works around this weakness.

Right around this time you should be wondering what that file looks like, so here are the contents:

rem CPAPPEND.BAT
rem
rem Copyright 1999-2004 The Apache Software Foundation
rem 
rem Licensed under the Apache License, Version 2.0 (the "License");
rem you may not use this file except in compliance with 
rem the License.
rem You may obtain a copy of the License at
rem 
rem http://www.apache.org/licenses/LICENSE-2.0
rem 
rem Unless required by applicable law or agreed to in writing, 
rem software distributed under the License is distributed on an 
rem "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
rem OF ANY KIND, either express or implied.
rem See the License for the specific language governing 
rem permissions and limitations under the License.
rem
@echo off
set JARS=%JARS%;%1

I've left the original license in the file to credit the source of this solution. As you can see it really only has one useful line, the last one. It just adds the parameter I call it with to a variable named JARS, and this is the same variable I use in the first (calling) script to later build my CLASSPATH variable.

So, if you want to build a CLASSPATH, or PATH, or any other environment variable dynamically in DOS (Windows CMD.EXE) this idea should do the trick for you.