views:

844

answers:

2

On a Unix systems it's very easy to compile the CLASSPATH by using find:

LIBDIR=`find lib/ -name \*.jar`
for DIR in $LIBDIR:
do
    CLASSPATH="$CLASSPATH:$DIR"
done

java -classpath $CLASSPATH com.example.MyClass

What would be the aquivalent in a Windows batchfile?

+4  A: 

The same can be achieved from Windows XP on with:

setlocal ENABLEDELAYEDEXPANSION
FOR /R .\lib %%G IN (*.jar) DO set CLASSPATH=!CLASSPATH!;%%G

java -classpath %CLASSPATH% com.example.MyClass
MrG
This doesn't work in Windows XP, unless you've run cmd /v first
kgiannakakis
Indeed it does if you set the ENABLEDELAYEDEXPANSION variable as described above (I forgot to add it initially, hence the edit) - just tested it seconds ago.
MrG
Bare in mind that if your classpath is long, then you can end up with an "input line too long" error message on Windows.... Maybe a way for Micro$oft to force people to actually point-and-click a runnable jar rather than the (apparently) evil pro-linux command line way of doing things :-)
DavidM
+1  A: 

The only way to do it is to use delayed environment variable expansion. You can do that by running

cmd /v

Then you can run:

set var=.
FOR /r %%d IN (*.jar) DO SET var=!var!;%%d
set CLASSPATH=%var%
kgiannakakis