?
Solved

Batch file to set CLASSPATH for Java

Posted on 2003-03-24
4
Medium Priority
?
7,956 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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