Sunday, May 25, 2008

Create a classpath dynamically

In order to create a classpath dynamically, containing all jars in a certain directory lib, from within a batch file (Windows), just do the following:

  1. Create a cpAppend.bat file, just containing:

    set CLASSPATH=%CLASSPATH%;%1


  2. In the runnable batch file, add the following to construct the path dynamically:

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


    The variable CLASSPATH will now contain all jars in the lib dir

That's it! On Linux/Unix the method is very similar.

No comments: