本文主要介绍通过Bat脚本自动下载、自动安装、自动配置Jmeter&JDK!

echo=1/*>nul&@cls
@echo off
:: 由于脚本内使用了bitsadmin下载器,故可能会被做病毒杀掉,若出现此类情况请关闭杀毒软件
:: 脚本更适合安装全新环境Jmeter&Jdk,其它环境也做了部分兼容,但可能兼容不完全
:: 若配置过程中出现下载问题,可手动准备相应工具至tools目录
:: 此脚本默认为配置X64 Windows操作系统,若需在32位操作系统配置此环境手动替换链接即可
:: X64文件下载链接如下:
:: https://eternallybored.org/misc/wget/1.20.3/64/wget.exe
:: https://files.cnblogs.com/files/ldylxy/7z64.zip
:: http://mirrors.tuna.tsinghua.edu.cn/apache//jmeter/binaries/apache-jmeter-5.1.1.zip
:: https://download.oracle.com/otn-pub/java/jdk/13+33/5b8a42f3905b406298b72d750b6919f6/jdk-13_windows-x64_bin.exe
:: X86文件下载链接如下:
:: https://eternallybored.org/misc/wget/1.20.3/32/wget.exe
:: https://files.cnblogs.com/files/ldylxy/7z32.zip

set wget=%cd%\tools\wget.exe
set wgetHttp=https://eternallybored.org/misc/wget/1.20.3/64/wget.exe

set zip=%cd%\tools\7z.exe
set zipHttp=https://files.cnblogs.com/files/ldylxy/7z64.zip

set jmeter=%cd%\tools\jmeter.zip
set jmeterHttp=http://mirrors.tuna.tsinghua.edu.cn/apache//jmeter/binaries/apache-jmeter-5.1.1.zip
set jmeterPath=C:\tools\Jmeter
set jmeterEnv=C:\tools\Jmeter\apache-jmeter-5.1.1

set jdk=%cd%\tools\jdk.exe
set jdkHttp=https://download.oracle.com/otn-pub/java/jdk/13+33/5b8a42f3905b406298b72d750b6919f6/jdk-13_windows-x64_bin.exe
set jdkPath=C:\tools\Java


:cha
:: 前置条件检查
:: 检查是否存在wget下载器
:: 检查是否存在7z命令行版本

cls
title Jmeter自动安装/环境检测
if not exist "tools" mkdir "tools"
if not exist %wget% call :bitsDownload %wgetHttp% %wget%
if not exist %wget% echo\
if not exist %wget% echo\ =======================================
if not exist %wget% echo\
if not exist %wget% echo wget配置失败请手动下载并放置于说明目录内
if not exist %wget% echo\
if not exist %wget% echo wget下载地址:%wgetHttp%
if not exist %wget% echo\
if not exist %wget% echo wget安装路径:%wget%
if not exist %zip% echo\
if not exist %zip% echo\
call :wgetDownload %zipHttp% %zip%

:autoInstall
:: 通过环境变量判断是否安装Jmeter&Jdk

cls
title Jmeter自动安装/自动安装
if "%JAVA_HOME%"=="" (
if "%JMETER_HOME%"=="" (
goto installAll
) else (
goto installJdk
)
) else (
if "%JMETER_HOME%"=="" (
goto installJmeter
) else (
goto end
)
)

:installAll
:: 下载Jmeter和Jdk
:: 安装Jmeter和Jdk
:: 设置Jmeter和Jdk环境变量

call :wgetDownload %jmeterHttp% %jmeter%
call :wgetDownload %jdkHttp% %jdk%
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
title Jmeter自动安装/安装Jmeter和Jdk
cd /d "%~dp0"
echo.
echo.
echo ***************开始安装Jmeter***************
echo.
echo 请不要执行其他操作
echo.
echo 安装过程需要三、四分钟,请稍等……
echo.
start /WAIT %jdk% /qn INSTALLDIR=%jdkPath%
echo.
echo %zip% x -y -o%jmeterPath% %jmeter%
%zip% x -y -o%jmeterPath% %jmeter%
echo.
echo ***************Jmeter安装完成***************
echo.
echo.
echo.
echo ***************开始配置Jmeter环境变量***************
echo.
:: 设置JDK环境变量JAVA_HOME
setx JAVA_HOME "%jdkPath%" /M
echo 创建JAVA_HOME变量,添加%jdkPath%
echo.
:: 设置Jmeter环境变量JMETER_HOME
setx JMETER_HOME "%jmeterEnv%" /M
echo 创建JMETER_HOME变量,添加%jmeterEnv%
echo.
:: 设置Jmeter环境变量Path
setx Path "%Path%;%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin;%%JMETER_HOME%%\bin" /M
echo Path变量内添加%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin;%%JMETER_HOME%%\bin
echo.
:: 设置Jmeter环境变量CLASSPATH
setx CLASSPATH "%CLASSPATH%;%%JAVA_HOME%%\lib\dt.jar;%%JAVA_HOME%%\lib\tools.jar;%%JMETER_HOME%%\lib\ext\ApacheJMeter_core.jar;%%JMETER_HOME%%\lib\jorphan.jar" /M
echo CLASSPATH变量内添加%%JAVA_HOME%%\lib\dt.jar;%%JAVA_HOME%%\lib\tools.jar;%%JMETER_HOME%%\lib\ext\ApacheJMeter_core.jar;%%JMETER_HOME%%\lib\jorphan.jar
echo.
echo ***************Jmeter环境变量配置完成***************
echo.
echo.
echo.
goto end

:installJdk
:: 下载Jdk
:: 安装Jdk
:: 设置Jdk环境变量

call :wgetDownload %jdkHttp% %jdk%
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
title Jmeter自动安装/安装Jdk
cd /d "%~dp0"
echo.
echo.
echo ***************开始安装JDK***************
echo.
echo 请不要执行其他操作
echo.
echo 安装过程需要三、四分钟,请稍等……
echo.
start /WAIT %jdk% /qn INSTALLDIR=%jdkPath%
echo.
echo ***************JDK安装完成***************
echo.
echo.
echo.
echo ***************开始配置JDK环境变量***************
echo.
:: 设置JDK环境变量JAVA_HOME
setx JAVA_HOME "%jdkPath%" /M
echo 创建JAVA_HOME变量,添加%jdkPath%
echo.
:: 设置JDK环境变量Path
setx Path "%Path%;%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin" /M
echo Path变量内添加%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin
echo.
:: 设置JDK环境变量CLASSPATH
setx CLASSPATH "%CLASSPATH%;%%JAVA_HOME%%\lib\dt.jar;%%JAVA_HOME%%\lib\tools.jar" /M
echo CLASSPATH变量内添加%%JAVA_HOME%%\lib\dt.jar;%%JAVA_HOME%%\lib\tools.jar
echo.
echo ***************JDK环境变量配置完成***************
echo.
echo.
echo.
goto end

:installJmeter
:: 下载Jmeter
:: 安装Jmeter
:: 设置Jmeter环境变量

call :wgetDownload %jmeterHttp% %jmeter%
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
title Jmeter自动安装/安装Jmeter
cd /d "%~dp0"
echo.
echo.
echo ***************开始安装Jmeter***************
echo.
echo 请不要执行其他操作
echo.
echo 过程很快,请稍等……
echo.
%zip% x -y -o%jmeterPath% %jmeter%
echo.
echo ***************Jmeter安装完成***************
echo.
echo.
echo.
echo ***************开始配置Jmeter环境变量***************
echo.
:: 设置Jmeter环境变量JMETER_HOME
setx JMETER_HOME "%jmeterEnv%" /M
echo 创建JMETER_HOME变量,添加%jmeterEnv%
echo.
:: 设置Jmeter环境变量Path
setx Path "%Path%;%%JMETER_HOME%%\bin" /M
echo Path变量内添加%%JMETER_HOME%%\bin
echo.
:: 设置Jmeter环境变量CLASSPATH
setx CLASSPATH "%CLASSPATH%;%%JMETER_HOME%%\lib\ext\ApacheJMeter_core.jar;%%JMETER_HOME%%\lib\jorphan.jar" /M
echo CLASSPATH变量内添加%%JMETER_HOME%%\lib\ext\ApacheJMeter_core.jar;%%JMETER_HOME%%\lib\jorphan.jar
echo.
echo ***************Jmeter环境变量配置完成***************
echo.
echo.
echo.
goto end

:bitsDownload
:: bits下载器
:: bits设置优先级:FOREGROUND、HIGH、NORMAL、LOW

title bitsadmin下载器
for %%a in (%~2) do (
set str=%%~pda
set var=%%~nxa
)
echo.
echo 文件获取路径:%~1
echo.
echo 文件存放路径:%str%
echo.
echo 文件名称:%var%
echo.
bitsadmin /transfer 下载文件%var% /download /priority FOREGROUND "%~1" "%~2"
goto :eof

:wgetDownload
:: wget下载器
:: wget怎么用自己脑补

title wget下载器
for %%a in (%~2) do (
set str=%%~pda
set var=%%~nxa
)
echo.
echo 文件获取路径:%~1
echo.
echo 文件存放路径:%str%
echo.
echo 文件名称:%var%
echo.
%wget% --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" -c %~1 -O %~2
goto :eof

:end
echo .
echo ************************************************
echo ***************Jmeter环境配置完成***************
echo ************************************************
pause

注释:保存脚本内容至以bat结尾文件内,运行bat脚本! 

一句话能解决的事情,绝对不要写一篇文章;一篇文章能解决的事情,绝对不要使用各种工具;一个工具能解决的事情,绝对不要跑东跑西……

02-13 11:07