如何从批处理脚本获取Java版本? [英] How to get Java Version from batch script?

查看:351
本文介绍了如何从批处理脚本获取Java版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得'6'如下

给出的Java版本的输出

  Java版本1.6.0_21
的Java(TM)SE运行时环境(建立1.6.0_21-B07)
Java的热点(TM)客户端虚拟机(建设17.0-B17,混合模式,共享)

有关同我写这个批处理脚本

 集版本6 =1.6.0_21
Java的版本2 - ;&放大器; 1 | FINDSTR版本> ab.txt
FOR / F令牌= 3中%%克(ab.txt)做(
  如果不是%%摹==%版本6%回声%%摹
  回声%%摹

%%摹显示1.6.0_21

有人五月指导我正确的方向?我没有太多熟悉的 FOR / F


解决方案

 关闭@echo
SETLOCAL设置版本6 =1.6.0_21
FOR / F令牌= 3中%%克('Java的版本2 ^> ^&放大器; 1 ^ | FINDSTR /我的版本')做(
    @echo输出:%%摹
    设置JAVAVER = %%摹

设置JAVAVER =%JAVAVER:=%
@echo输出:%JAVAVER%FOR / Fdelims =。=令牌1-3中(%JAVAVER%)%% v请勿(
    @echo专业:%% v
    @echo辅修:%%W¯¯
    @echo生成:%%点¯x
)ENDLOCAL

在第一个循环,令牌= 3说,我们将只使用从命令的输出第三令牌。而不是 Java的版本的输出命令重定向到一个文件,我们就可以运行循环中这个命令本身。插入记号( ^ )是转义字符,并且需要,所以我们可以嵌入> &放大器; | 命令字符串符号

循环体中,我们制定了一个新的变种, JAVAVER ,这样我们就可以做一些操纵的版本字符串后面。

设置JAVAVER =%JAVAVER:=%命令来自各地的版本字符串删除双引号

最后一个循环解析Java版本字符串。 delims = 表示,我们将使用时间分隔标记。 标记= 1-3 说,我们要前三令牌从字符串传递到循环体。现在我们可以使用明确的变量, %% v 和隐含变量(字母表中的下一个字母)%的Java版本字符串的组成部分%重量 %% X

当我在我的系统上运行此我得到:

 输出:1.6.0_24
输出:1.6.0_24
专业:1
辅修:6
构建:0_24

I am trying to get '6' out of the java version output given below

java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)

For the same I wrote this batch script

set VERSION6="1.6.0_21"
java -version 2>&1 | findstr "version" >ab.txt
for /f "tokens=3" %%g in (ab.txt) do (
  if not %%g == %VERSION6% echo %%g
  echo %%g
)

%%g displays "1.6.0_21"

May someone guide me to correct direction? I am not much familiar with for /f.

解决方案

@echo off
setlocal

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

for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
    @echo Major: %%v
    @echo Minor: %%w
    @echo Build: %%x
)

endlocal

In the first for loop, "tokens=3" says that we're going to just use the third token from the command output. Rather than redirect the output of the java -version command to a file, we can run this command within the for loop itself. The carets (^) are escape characters, and are needed so we can embed the >, & and | symbols in the command string.

Within the body of the for loop, we set a new var, JAVAVER, so that we can do some manipulation of the version string later.

The set JAVAVER=%JAVAVER:"=% command removes the double quotes from around the version string.

The last for loop parses the java version string. delims=. says we're going to delimit tokens using periods. tokens=1-3 says we're going to pass the first three tokens from the string to the body of the loop. We can now get the components of the java version string using the explicit variable, %%v and the implied variables (next letters in the alphabet) %%w and %%x.

When I run this on my system I get:

Output: "1.6.0_24" 
Output: 1.6.0_24
Major: 1 
Minor: 6 
Build: 0_24

这篇关于如何从批处理脚本获取Java版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆