Generating a classpath to include all .jar files in MS-DOS

Interesting, isn’t it, that DOS stands for ‘Denial of Service’ as well as ‘Disk Operating System’?

I recently had a problem whereby I needed to write a .bat file to launch a java application from DOS. The ’simple’ requirement was to generate a classpath on the fly which consisted of all the jar files packaged in the applications local ‘lib’ directory.

Well, this would be simple on ‘nix, but in DOS it is another story.

Eventually I found a way to write launch script which runs the app by first auto-generating a runscript.bat with the correct classpath, which it then invokes. This works pretty well, and I couldn’t Google myself a better solution from anywhere, so here is my best effort – but there must be some better way to do it??

(n.b. update Oct 2008 – for a better solution, see Irha’s comment below – haven’t tried it but it looks promising to me!)

echo SET CP > runscript.bat

for %%i in (..\lib\*.jar) DO echo SET CP=%%i;%%CP%% >> runscript.bat

echo java -classpath %%CP%% com.od.MyApp >> runscript.bat
echo pause >> runscript.bat
runscript.bat &

n.b. In jdk 1.6+ this would be unnecessary, since there is a new syntax which lets you use a classpath element with a basename of ‘*’ to refer to all jars in a directory



You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

2 Responses to “Generating a classpath to include all .jar files in MS-DOS”

  1. For the record, here is a simpler way to do this by using delayed expansion of variables.

    setlocal ENABLEDELAYEDEXPANSION
    for %%I IN (..lib*.jar) DO SET CP=!CP!;%%I

  2. Thanks Irha, that does indeed look like it might be a better way, and the setlocal ENABLEDELAYEDEXPANSION is a really useful bit of knowledge

Leave a Reply