Friday, October 8, 2010

First Batch Script setlocal EnableDelayedExpansion

Needed to create a batch script to kick off a Java program. Had never written a batch script before and had some troubles getting the class path set properly. The code I was using was:

@echo on
if "%JAVA_HOME%"=="" (
echo The JAVA_HOME environment variable is not set.
goto end
)

set TEMP_CLASSPATH=.;
for %%i in (..\lib\*.jar) do (
set TEMP_CLASSPATH=!TEMP_CLASSPATH!;%%i
)

set TEMP_CLASSPATH=!TEMP_CLASSPATH!.;
echo %TEMP_CLASSPATH%

java -cp %TEMP_CLASSPATH% com.amphibiousrodents.MyJavaProgram
pause

I wrote it in Windows XP (not sure if this has any bearing on anything). When I ran this script from my Eclipse project's /bin folder it was causing a java.lang.NoClassDefFoundError

After some digging around someone mentioned that adding a line:
setlocal EnableDelayedExpansion would do the trick. After adding that into the .bat file at the top, the classpath was properly set and the program ran without problem. setlocal EnableDelayedExpansion......hmmmm......shoe

No comments:

Post a Comment