The Artima Developer Community
Sponsored Link

Java Answers Forum
"for loop : problem in batch file

6 replies on 1 page. Most recent reply: Oct 27, 2005 11:14 AM by Jonathan Eid

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
nabakumar

Posts: 23
Nickname: nkmeitei
Registered: Jun, 2005

"for loop : problem in batch file Posted: Jul 24, 2005 11:38 PM
Reply to this message Reply
Advertisement
I am trying to append all the jars in the classpath from the lib folder ...
for that i have made a batch file as given below . But the problem is the value of CP is getting overwritten in each loop rather than appending .can anybody point out where it went wrong....

set JARS=D:\ant\apache-ant-1.6.1\lib
set CP=.
for %%i in ("%JARS%\*.jar") do set CP=%CP%;%%i

set classpath=%classpath%;%CP%


naba


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: "for loop : problem in batch file Posted: Jul 25, 2005 5:45 AM
Reply to this message Reply
> I am trying to append all the jars in the classpath from
> the lib folder ...
> for that i have made a batch file as given below . But the
> problem is the value of CP is getting overwritten in each
> loop rather than appending .can anybody point out where it
> went wrong....
>
> set JARS=D:\ant\apache-ant-1.6.1\lib
> set CP=.
> for %%i in ("%JARS%\*.jar") do set CP=%CP%;%%i
>
> set classpath=%classpath%;%CP%
>
>
> naba

Not a hundred percent sure but I believe try:
using colon in place of ; e.g

for %%i in ("%JARS%\*.jar") do set CP=%CP%:%%i

set classpath=%classpath%:%CP%

You separate iterative elements in Unix by
a colon ":" not a semicolon ";"

Path elements in Windows:

C:\path1;C:\path2

In unix:

path1:path2:etc

Haven't done the Jarring before, but I have been
setting up Classpaths for a while under Linux,
so I'm not sure if this applies to your particular
problem, but its worth a shot.

Hope it works out.

Spike

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: "for loop : problem in batch file Posted: Jul 28, 2005 10:20 AM
Reply to this message Reply
This is a bit convoluted and there may be a more clever way of doing this than creating the tempoary batch file (if there is some way of transferring a variable's value outside a setlocal scope), but it'll work:
@echo off
setlocal EnableDelayedExpansion
set JARS=D:\ant\apache-ant-1.6.1\lib
set CP=.
for %%i in ("%JARS%\*.jar") do set CP=!CP!;%%i
set classpath=%classpath%;%CP%
echo Right classpath: %classpath%
echo @set classpath=%classpath%>%temp%\SetClasspath.cmd
endlocal
call %temp%\SetClasspath.cmd
del %temp%SetClassPath.cmd
echo Classpath is now %classpath%
The important parts to note are the setlocal EnableDelayedExpansion, the use of !CP! instead of %CP% inside the for loop and (particularly the placement of) endlocal.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: "for loop : problem in batch file Posted: Jul 28, 2005 11:27 PM
Reply to this message Reply
Hey Matt:

Just hoping for an opportunity to learn from the top
dawgs in the industry:

You have:

set classpath=%classpath%;%CP%

I was under the impression that in *nix systems,
path variables are separated by ":" and not ";"

If possible could you also clarify what:

"setlocal EnableDelayedExpansion"

does.

Tx,
Spike

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: "for loop : problem in batch file Posted: Jul 29, 2005 4:27 PM
Reply to this message Reply
Hi Kondwani,

Yes, on Unix-based systems you use the colon, but Windows-based systems, you use semicolon. Based on what the OP had written, I thought it was Windows not Unix.

By the way, I found out the way do do it without a temporary file. It is pretty arcane, but here it is:
setlocal EnableDelayedExpansion
set JarsDir=D:\ant\apache-ant-1.6.1\lib
if defined classpath (set classpath=%classpath%;.) else (set classpath=.)
for %%i in ("%JarsDir%\*.jar") do set classpath=!classpath!;%%i
endlocal & set classpath=%classpath%
The trick is that if you chain the set command to the to the endlocal command, you are in some quasi-state where you can set variables in the outer environment from the inner one. Pretty strange, but it works.

eashwar

Posts: 1
Nickname: eashwar
Registered: Oct, 2005

Re: "for loop : problem in batch file Posted: Oct 3, 2005 11:45 PM
Reply to this message Reply
The above solutions however do not work in Win 2000. I found that it works in win XP. So is there any solution for win 2000?

In Win 2000, it appends only few files two or three and not more than that.

Jonathan Eid

Posts: 1
Nickname: jeid
Registered: Sep, 2005

Re: "for loop : problem in batch file Posted: Oct 27, 2005 11:14 AM
Reply to this message Reply
I recently solved this in the following manner. These work on WindowsXP Pro and Windows Server 2003. Lots of credit has to go to the Apache Jakarta Tomcat crew. I got lot's of ideas from the Tomcat startup scripts

There are two batch files, one of which is "included" by another. The "included" batch file is used to set the class path for the "wrapper" batch/cmd file that actually starts the JVM with your class.

------------------ 8< snip! ------------------------
REM setClasspath.bat
@echo off
set LIB_CLASSPATH=%DASHBOARD_HOME%\bin
rem FOR %%f IN (lib\*.jar) DO echo %%f
REM ******************************************************
REM Environment variables within a FOR loop are expanded
REM at the beginning of the loop and won't change until
REM AFTER the end of the DO section.
REM Need to use the CALL :subroutine mechanism
REM ******************************************************
REM FOR %%f IN (lib\*.jar) DO (call :append_classpath "%%f")
FOR %%f IN (%DASHBOARD_HOME%\lib\*.jar) DO (call :append_classpath %%f)
rem echo %LIB_CLASSPATH%

:append_classpath
rem echo LIB_CLASSPATH:%LIB_CLASSPATH% - arg:%1
set LIB_CLASSPATH=%LIB_CLASSPATH%;%1
GOTO :eof
------------------ 8< snip! ------------------------
Name this following file whatever you want (foo.bat,foo.cmd)
------------------ 8< snip! ------------------------

@echo off
setlocal
rem
rem Wrapper script to run [JavaApplicationWithMainMethod]
rem
if "%OS%"=="Windows_NT" goto nt
echo This script only works with NT-based versions of Windows.
goto :eof

:nt
rem
rem Find the application home.
rem
rem %~dp0 is location of current script under NT
set _REALPATH=%~dp0
cd ..
set APPLICATION_HOME=%cd%

rem echo %APPLICATION_HOME%
rem echo _REALPATH == %_REALPATH%

if exist "%APPLICATION_HOME%\bin\setClasspath.bat" call "%APPLICATION_HOME%\bin\setClasspath.bat"
set CLASSPATH=%LIB_CLASSPATH%

rem echo CLASSPATH = %CLASSPATH%

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs

:doneSetArgs
rem
rem Start java program
rem
:startup
java -cp %CLASSPATH% [your.fully.qualifed.class.name.goes.Here] %CMD_LINE_ARGS%
rem The setlocal command also sets the errorlevel value
rem when it is passed an argument.
rem The errorlevel value is set to zero (0) if one of the
rem two valid arguments is given and set to one (1)
rem otherwise.
rem if not errorlevel 1 goto :eof
:eof
------------------ 8< snip! ------------------------

Flat View: This topic has 6 replies on 1 page
Topic: regarding my query on tracking the selected checkbox values across the pagi Previous Topic   Next Topic Topic: Bill Please take Action on the laziness involved in Code Posting

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use