Skip to main content

Setting classpath dynamically

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2011-04-12

From a Windows batch script, I am trying to set the classpath to include all the jars in a particular directory without hardcoding the list of jars in the batch script. Therefore, when a new jar is added to this directory, the batch script will not need to be modified. Is there an easy way to do this? Thanks for your help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2011-04-11

I use 2 batch scripts since expanding an environment variable step by step seems to be not working in just one loop command:
addjarstocp.cmd is coded as follows:
@echo off
FOR %%f IN (%1*.jar) DO call addtocp %%f

and addtocp.cmd looks like:

A sample call would e.g. be:
addjarstocp d:\v\util\

This adds all *.JAR files found in d:\v\util\ to the end of the CLASSPATH.
Mind the backslash at the end of d:\v\util\
This is needed since I am not adding a directory separator inside addjarstocp.cmd. Reason: if I added it there, the scipt could no longer be used without a parameter to add the local jars found in the current dir since it would always add a separator.