从.bat文件运行.jar文件,然后执行其他命令

时间:2015-07-23 04:37:26

标签: batch-file jar command-prompt

我正在执行一项任务,我必须使用批处理文件运行一个.jar文件,该文件由另一个批处理文件调用。执行后我需要删除一些垃圾文件。删除文件的代码写在同一个调用批处理文件中。

现在的问题是,一旦.jar文件开始执行,命令窗口关闭然后控制永远不会回到批处理文件执行,因此我的删除代码永远不会工作。

有没有办法解决这个问题。

由于 阿米特

以下是我正在使用的代码。

@Echo off

echo "your file is getting processed...."

md %userprofile%\temp

echo %userprofile%\temp


'"C:\Program Files\winzip\Winzip64.exe" -min -e -o C:\mysoft\Benchmarks\Windows7.zip %userprofile%\temp

C:
cd %userprofile%\temp\Windows7
echo going to execute file...

soft-CAT.bat -a -s -n -csv -b benchmarks\soft_Microsoft_Windows_7_Benchmark_v2.1.0-xccdf.xml -p "Level 1 + BitLocker" -r c:\

set folder="C:\test\junk"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

ECHO "Successfully Evaluated"

Exit

调用批处理文件

@ECHO OFF
::
:: Wrapper for invoking soft-CAT
::

SET DEBUG=0

SET JAVA=java

::
:: Detect if Java is in the path
::

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: If Java is not in the PATH, try finding it at JAVA_HOME\bin
::

SET JAVA=%JAVA_HOME%\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: See if x86 JRE5 is in the default location
::

SET JAVA=C:\PROGRA~2\Java\jre5\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: See if x86 JRE6 is in the default location
::

SET JAVA=C:\PROGRA~2\Java\jre6\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: See if x86 JRE7 is in the default location
::

SET JAVA=C:\PROGRA~2\Java\jre7\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: See if x86 JRE8 is in the default location
::

SET JAVA=C:\PROGRA~2\Java\jre8\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: See if JRE6 is in the default location
::

SET JAVA=C:\PROGRA~1\Java\jre6\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: See if JRE7 is in the default location
::

SET JAVA=C:\PROGRA~1\Java\jre7\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

::
:: See if JRE8 is in the default location
::

SET JAVA=C:\PROGRA~1\Java\jre8\bin\java.exe

%JAVA% 2> NUL > NUL

IF NOT %ERRORLEVEL%==9009 IF NOT %ERRORLEVEL%==3 GOTO RUNSOFTCAT

IF %ERRORLEVEL%==9009 GOTO NOJAVAERROR
IF %ERRORLEVEL%==3 GOTO NOJAVAERROR

::
:: Invoke SOFT-CAT with a 768MB heap
::

:RUNSOFTCAT

IF %DEBUG%==1 (
    ECHO Found Java at %JAVA%
    %JAVA% -Xmx768M -jar SOFTCAT.jar %* --verbose
) ELSE (
    %JAVA% -Xmx768M -jar SOFTCAT.jar %*
)
echo "I am in main file, execution is done...."
GOTO EXIT

:NOJAVAERROR

ECHO The Java runtime was not found in PATH, default install locations, or JAVA_HOME.  Please ensure Java is installed.
PAUSE

:EXIT

2 个答案:

答案 0 :(得分:0)

尝试将CALL放在批处理文件名前面,如下所示:

call soft-CAT.bat -a -s -n -csv -b benchmarks\soft_Microsoft_Windows_7_Benchmark_v2.1.0-xccdf.xml -p "Level 1 + BitLocker" -r c:\

非常好的信息是从另一个批处理文件调用批处理文件的不同方法:Several ways to call a windows batch file from another one or from prompt. Which one in which case?

答案 1 :(得分:0)

如果从批处理文件启动批处理文件,则替换后者与第一个批处理文件。如果您希望批处理文件作为子例程运行,则需要调用

REM this is FIRST.BAT
echo Now I call the second one
CALL SECOND.BAT
echo SECOND.BAT has finished!

REM this is SECOND.BAT
echo Doing things ...