How to get Java version in a batch script subroutine?

馋奶兔 提交于 2020-01-06 06:44:54

问题


From this question:

for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
    @echo Output: %%g
    set JAVAVER=%%g
)

How can I put this into a subroutine and call it, passing a path to the java executable? Here's an example of my problem:

@echo off
setlocal enabledelayedexpansion

call :GET_JAVA_VER "java"
goto END

:GET_JAVA_VER
    for /f "tokens=3" %%g in ('%1 -version 2^>^&1 ^| findstr /i "version"') do @echo %%g
    %1 -version 2>&1 | findstr /i "version"
goto :EOF

:END
    endlocal

This outputs:

The filename, directory name, or volume label syntax is incorrect.
java version "10.0.1" 2018-04-17

It seems to me that the for loop is evaluating the parameter (%1) differently.

What's the difference?


EDIT

Possible duplicate of Java Version in a batch file

According to this answer, if that is indeed the problem, I would have to somehow transform the passed in path by adding quotes around each directory containing spaces. That sounds like a pain and I'm hoping there's another explanation; I still don't get why in this case just passing in "java" doesn't work.

(Note: using usebackq and back-quotes makes no difference)


回答1:


It seems that piping the output to findstr was stripping the quotes for some reason. I deduce this because when I removed the ^| findstr /i "version" portion, this:

@echo off
setlocal enabledelayedexpansion

call :GET_JAVA_VER "java"
goto END

:GET_JAVA_VER
    for /f "tokens=3" %%g in ('%1 -version 2^>^&1') do @echo %%g
goto :EOF

:END
    endlocal

Outputs this:

"10.0.1"
Runtime
64-Bit

This modified script works (with full paths as well):

@echo off
setlocal enabledelayedexpansion

call:GET_JAVA_VER "java"
goto END

:GET_JAVA_VER
    for /f "tokens=3" %%g in ('%~1 -version 2^>^&1') do (
        echo %%~g
        rem Stop after first line
        goto :EOF
    )
goto :EOF

:END
    endlocal


来源:https://stackoverflow.com/questions/50651070/how-to-get-java-version-in-a-batch-script-subroutine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!