How to dynamically build the CLASSPATH environment variable in DOS (Windows Command Line)

Nowadays, in the era of wrapper-exe’s it seams that most of the people had forgotten how to kick start their Java APP using some clever batch scripts. I still remember the day when the Aragon Faramework was using these batch scripts to start…hmm..the good old days.

Usually the biggest challenge of starting a java program is to build up the CLASSPATH variable, specially when you are using tons of third-party jars.
Maintaining a script manually is a hard job…and I think that a developer should take care of the code he/she is writing rather than updating some nasty batch scripts.

So here is an example showing how to solve the problem…

Assuming that the third party jars are located in the lib directory.

Step 1. Create a batch file named start.cmd for instance, with the following content:

set JARS=set
for %%i in (lib\*.jar) do call cpappend.bat %%i
java -cp %JARS% my.app.package.MyMain

Step 2. Create the cpappend.bat file in the same place you have the start.cmd.

@echo off
set JARS=%JARS%;%1

Thats it.

By running the start.cmd the classpath of the application will be filled with all the jars found in the lib folder.

‘Till next time….

Happy coding

One thought on “How to dynamically build the CLASSPATH environment variable in DOS (Windows Command Line)

  1. Pingback: nWizard - Norbert Rakosi’s blog on Free Software development … JAVA … Personal Development… and … bad speling… » Free Java Executable Wrappers