Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Batch file to set CLASSPATH for Java

Posted on 2003-03-24
4
Medium Priority
?
8,008 Views
Last Modified: 2009-12-17
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?

0
Comment
Question by:nsen
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
billious earned 200 total points
ID: 8199836
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 8203214
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
 
LVL 7

Expert Comment

by:billious
ID: 8203489
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
 

Author Comment

by:nsen
ID: 8204572
This works like a charm (except the syntax change highlighted in the next post).
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question