Batch file to set CLASSPATH for Java

I want to add all the jar files in a directory to my CLASSPATH from my batch file. I tried something like:

set PRE_CLASSPATH=
for %%i in (c:\java\lib\*.jar) do set PRE_CLASSPATH=%%i;%PRE_CLASSPATH%
echo %PRE_CLASSPATH%

But the PRE_CLASSPATH does not contain all the files in the lib directory but only the last file. Any idea what the issue is? What's a descent workable solution?

nsenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

billiousCommented:
Yeah, it'll do that.

FOR appears to take a copy of the current environment and commit the changes only when it terminates.

cure: call an internal routine

------------------------------------------
set PRE_CLASSPATH=
for %%i in (c:\java\lib\*.jar) do call setclass %%i
echo %PRE_CLASSPATH%
goto :eof

:setclass
set PRE_CLASSPATH=%1;%PRE_CLASSPATH%
goto :eof
---------------------------------------------

(all in your batch - not two separate batches)

Note :- If you use spaces in your filenames, you would need to use

call setclass "%%i"

rather than

call setclass %%i


the label :eof need not be declared - IF you use
"goto :eof"

...Bill
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveGTRCommented:
You might try:

@echo off

setlocal ENABLEDELAYEDEXPANSION

set PRE_CLASSPATH=
for %%i in (c:\java\lib\*.jar) do  set PRE_CLASSPATH=%%i;!PRE_CLASSPATH!

echo %PRE_CLASSPATH%

Or, you could use Bill's suggestion with the following syntax fixes:

@echo off

setlocal ENABLEDELAYEDEXPANSION

set PRE_CLASSPATH=
for %%i in (c:\java\lib\*.jar) do call :setclass %%i

echo %PRE_CLASSPATH%
goto :eof

:setclass
set PRE_CLASSPATH=%1;%PRE_CLASSPATH%

:eof

But, as Bill stated his code will fail if there is any spaces in the file names. So this would work:

@echo off

setlocal ENABLEDELAYEDEXPANSION

set PRE_CLASSPATH=
for %%i in (c:\java\lib\*.jar) do (
  set jarfile=%%i
  call :setclass
)  

echo %PRE_CLASSPATH%
goto :eof

:setclass
set PRE_CLASSPATH=%jarfile%;%PRE_CLASSPATH%
set jarfile=

:eof

Good Luck,
Steve


0
billiousCommented:
SteveGTR:

Pity ENABLEDELAYEXPANSIONS (time for a coffee break...) wasn't available in NT.

Using spaces in file/directory names is a dumb idea IMVHO, and I never use it. Have to deal with others who have different opinions (however weird) though...

...Bill
0
nsenAuthor Commented:
This works like a charm (except the syntax change highlighted in the next post).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.