现在我正在使用两个批处理文件,在本例中我们称之为variables.cmd和main.cmd。 variable.cmd文件用于外部化可能与多个其他批处理文件一起使用的一组变量。
现在除了CLASSPATH之外,在main.cmd文件中访问每个变量都没有问题。当在variables.cmd中调用echo时,CLASSPATH被正确创建,但是当在main.cmd文件中回显时,我得到以下结果:
!CLASSPATH!;C:\ipp\deploy\lib\app\momex-webservice-client.jar;C:\ipp\deploy\resources
关于最新情况以及如何解决的任何想法?
这是main.cmd文件:
@echo off
call variables.cmd
echo %JAVA_HOME%
echo %DEPLOY%
echo %DEBUG%
echo %DEBUG_PORT%
echo %JAVA_OPTIONS%
echo %CLASSPATH%
这是variables.cmd文件:
@echo off
setlocal EnableDelayedExpansion
set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_30"
set DEPLOY=C:\ipp\deploy
set DEBUG=false
set DEBUG_PORT=15000
set JAVA_OPTIONS="
set JAVA_OPTIONS=%JAVA_OPTIONS% -Djava.endorsed.dirs=%DEPLOY%\lib\endorsed
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump="true"
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dconfiguration.properties=%DEPLOY%\resources\vendor.configuration.properties
if "%DEBUG%"=="true" (
set JAVA_OPTIONS=%JAVA_OPTIONS% -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=15000,suspend=n,server=y
)
set CLASSPATH=%DEPLOY%
for %%i in (%DEPLOY%\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
for %%i in (%DEPLOY%\lib\system\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
for %%i in (%DEPLOY%\lib\app\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
for %%i in (%DEPLOY%\tools\jaxws-ri\lib\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
set CLASSPATH=%CLASSPATH%;%DEPLOY%\resources
答案 0 :(得分:1)
--main.bat--
@echo in main.bat
@set CLASSPATH="This is a failed test"
@echo The current classpath is %CLASSPATH%
@echo going to call variables.bat
@call variables.bat
@call temporary.bat
@echo back in main.bat
@echo The current classpath is %CLASSPATH%
@echo main.bat -- all done
--variables.bat--
@setlocal EnableDelayedExpansion
@echo In variables.bat
@set something="This is a successfull test"
@echo set classpath=!something! > temporary.bat
@endlocal
@echo variables.bat is finished
答案 1 :(得分:1)
这是一个经过修改的variabled.cmd文件:
@echo off
set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_30"
set DEPLOY=C:\ipp\deploy
set DEBUG=false
set DEBUG_PORT=15000
set JAVA_OPTIONS="
set JAVA_OPTIONS=%JAVA_OPTIONS% -Djava.endorsed.dirs=%DEPLOY%\lib\endorsed
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump="true"
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dconfiguration.properties=%DEPLOY%\resources\vendor.configuration.properties
if "%DEBUG%"=="true" (
set JAVA_OPTIONS=%JAVA_OPTIONS% -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=15000,suspend=n,server=y
)
set CLASSPATH=%DEPLOY%
setlocal EnableDelayedExpansion
for %%i in (%DEPLOY%\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
for %%i in (%DEPLOY%\lib\system\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
for %%i in (%DEPLOY%\lib\app\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
for %%i in (%DEPLOY%\tools\jaxws-ri\lib\*.jar) do (
set CLASSPATH=!CLASSPATH!;%%i
)
endlocal & set CLASSPATH=%CLASSPATH%;%DEPLOY%\resources
唯一的变化是setlocal
命令和endlocal
在最后一组CLASSPATH变量的同一行中的位置。这样,此批处理文件中设置的值将保留为main.bat文件,并在需要时启用延迟扩展。
答案 2 :(得分:0)
variables.cmd顶部的setlocal特别导致其中定义的变量为“local” - 因此不在main.cmd的“范围内”