识别是否无法在Windows批处理脚本中执行Java [英] identify if java cannot be executed in windows batch script
问题描述
我写了下面的批处理脚本,如果环境中不存在 JAVA_HOME
路径,它会询问它,然后验证Java版本.但是在确定Java版本之前,还应该检查路径中是否存在 java
(可以执行).请帮我弄清楚是否可以执行 java -version
并显示正确的消息
I wrote the below batch script which asks for a JAVA_HOME
path if its not present in environment, and then it verifies the java version. but before identifying java version it should also check whether java
is present in the path (can be executed) or not. Please help me figure out if java -version
can be executed or not and display proper message
@echo off
setlocal
set VERSION5="1.5"
IF "%JAVA_HOME%" == "" (
echo Enter path to JAVA_HOME:
set /p JAVA_HOME=
goto check_java_version
) ELSE (
echo Using %JAVA_HOME%
goto check_java_version
)
:check_java_version
for /f "tokens=3" %%g in ('%JAVA_HOME%\jre\bin\java -version 2^>^&1 ^| findstr /i "version"') do (
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
set JAVAVER=%JAVAVER:java version =%
for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
set VER=%%w
)
if not "%VER%" GEQ "5" goto wrong_version
set JAVA_BIN=%JAVA_HOME%\jre\bin
goto correct_java_version
:correct_java_version
REM echo JAVA Version is ok.
set JAVA_LIB=%cd%/lib
%JAVA_BIN%/java -cp %JAVA_LIB%/csm-runtime-1.0.jar;%JAVA_LIB%/groovy-all-1.8.1.jar;%JAVA_LIB%/commons-beanutils-1.8.3.jar;%JAVA_LIB%/csm-dbutil-1.0.jar;%JAVA_LIB%/commons-exec-1.1.jar;%JAVA_LIB%/log4j-1.2.8.jar;%JAVA_LIB%/commons-cli-1.2.jar -Dlog4j.configuration=com/ABC/csm/log4j.xml -Dendorsed_plugins_dir=./plugins com.ABC.csm.CSMMain %*
goto end_java_version
:wrong_version
echo Current JDK Version %VER%
echo Expected JDK %VERSION5% or greater. Please fix your SSATOP and try again.
goto end_java_version
:no_java
echo No JDK found in %JAVA_HOME%.
goto wrong_version
:end_java_version
endlocal
1个无效条件示例将是,而不是提供 JAVA_HOME
,即e:\ csm \ java我将其指定为e:\ csm \ java \ jre \ bin应该显示正确的错误消息,请请提供一个JAVA_HOME
路径
1 of the examples of invalid condition would be, instead of providing JAVA_HOME
i.e., e:\csm\java I gave it as e:\csm\java\jre\bin which in this case should display proper error message that please provide a JAVA_HOME
path
推荐答案
要检查 PATH
中是否存在程序,Windows批处理将提供〜%PATH:
SET
命令的选项.请参见帮助呼叫
或帮助
.
To check the existence of a program in the PATH
, windows batch provides the ~%PATH:
option of the SET
command. See HELP CALL
or HELP FOR
.
以这段代码为起点.
:ProgInPath
set PROG=%~$PATH:1
goto :eof
并像这样使用它
call :ProgInPath java.exe
IF "%PROG%" == "" (
echo Java.exe not found
) else (
echo. %PROG%
)
在此示例中,如果 java.exe
位于 PATH
中,它将回显其完整的文件规范.
in this example, if java.exe
is in the PATH
, it echoes its complete filespec.
这篇关于识别是否无法在Windows批处理脚本中执行Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!