posts - 41,  comments - 40,  trackbacks - 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Author: NeedJava
::
:: Modified: 2007-07-07
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO OFF


SETLOCAL EnableDelayedExpansion

ECHO 搜索可用的〔JRE〕路径,按任意键继续。

PAUSE>NUL


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: 先找到java.exe文件,得到主要路径,然后根据java.exe附近一些有特点的文件,
::
:: 来判断是否是有效的路径。
::
:: 没办法识别人为制造的恶意路径,除非每个文件都用MD5辨认。
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
DEL /F /Q JRE.txt 2>NUL

FOR %%a IN ( C D E F G H I J K L M N O P Q R S T U V W X Y Z ) DO (

    IF EXIST "%%a:\" (

       ECHO 正在〔%%a:〕盘下搜索……

       FOR /F "delims=" %%b IN ( 'DIR /A-D /B /S "%%a:\java.exe" 2^>NUL' ) DO (

           SET "JRELocation=%%~dpb"

REM        ECHO [%%~fb] & ECHO [!JRELocation!] & PAUSE

REM        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM        ::
REM        :: 去除末尾的“\bin\”
REM        ::
REM        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM        IF /I "!JRELocation:~5!"=="\bin\" (
REM
REM           SET "JRELocation=!JRELocation:~0,5!"
REM
REM           ECHO [!JRELocation!] & PAUSE
REM
REM        )

REM        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM        ::
REM        :: 这是JDK内含的JRE
REM        ::
REM        :: C:\Program Files\Java\jdk1.5.0_12\bin\java.exe
REM        ::
REM        :: C:\Program Files\Java\jdk1.5.0_12\jre\bin\java.dll
REM        ::
REM        :: C:\Program Files\Java\jdk1.5.0_12\jre\bin\client\jvm.dll
REM        ::
REM        :: C:\Program Files\Java\jdk1.5.0_12\jre\lib\rt.jar
REM        ::
REM        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
           IF EXIST "!JRELocation!\..\jre\bin\java.dll" (

              IF EXIST "!JRELocation!\..\jre\bin\client\jvm.dll" (

                 IF EXIST "!JRELocation!\..\jre\lib\rt.jar" (

                    CALL :version !JRELocation!

                 )

              )

           )

REM        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM        ::
REM        :: 这是单独的JRE,必须剔除JDK内含JRE的情况,防止重复
REM        ::
REM        :: C:\Program Files\Java\jre1.5.0_12\bin\java.exe
REM        ::
REM        :: C:\Program Files\Java\jre1.5.0_12\bin\java.dll
REM        ::
REM        :: C:\Program Files\Java\jre1.5.0_12\bin\client\jvm.dll
REM        ::
REM        :: C:\Program Files\Java\jre1.5.0_12\lib\rt.jar
REM        ::
REM        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
           IF EXIST "!JRELocation!\java.dll" (

              IF EXIST "!JRELocation!\client\jvm.dll" (

                 IF EXIST "!JRELocation!\..\lib\rt.jar" (

                    IF NOT EXIST "!JRELocation!\..\..\bin\java.exe" (

                       CALL :version !JRELocation!

                    )

                 )

              )

           )

       )

    )

)

GOTO :show


:version
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: JAVA.EXE -version不是用标准输出流,而是用标准错误输出流
::
:: 所以如果要输出到文件,必须用错误流,即2>。输出类似如下:
::
:: java version "1.5.0_12"
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO Found JRE: [%*]

IF NOT "!JRELocation!"=="" IF "!JRELocation!"=="%*" (

   "!JRELocation!\JAVA.EXE" -version 2>versionErr.txt 1>versionOut.txt

   FOR /F "tokens=1,2,3,4* delims= " %%a IN ( versionErr.txt versionOut.txt ) DO (

       IF /I "%%a"=="java" IF /I "%%b"=="version" (

          SET "JREVersion=%%~c"

          ECHO JRE Version: [!JREVersion!] & ECHO.

          ECHO !JREVersion!^ !JRELocation!>>JRE.txt

       )

   )

   DEL /F /Q versionErr.txt 2>NUL

   DEL /F /Q versionOut.txt 2>NUL

)

GOTO :EOF


:show
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: 不建议用START.EXE命令,防止关联被篡改,直接调用记事本NOTEPAD.EXE打开。
::
:: 其实后面用不着这个,这里只是为了方便查看。
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
IF EXIST JRE.txt "%SystemRoot%\NOTEPAD.EXE" JRE.txt


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: 下一步,就是自动设置环境变量path,使用INF文件改注册表,如下:
::
:: HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path:
::
:: "%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;"
::
:: 可以读出找到的JRE,让使用者自己选择用哪个JRE,用到的命令FOR、SET /P,以后再写……
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


@ECHO ON

 

 
posted on 2007-07-05 16:28 NeedJava 阅读(1139) 评论(0)  编辑  收藏 所属分类: DOS Windows

只有注册用户登录后才能发表评论。


网站导航: