通过bat脚本批处理设置JAVA_HOME, TOMCAT_HOME环境变量。
xp以前通过 wmic ENVIRONMENT来设置环境变量,不过这种设置方式没办法立即生效。
vista以后通过 setx 设置用户环境变量, 加入 -M参数可设置系统环境变量,并且是立即生效(新开命令窗口,原命令窗口不生效)
@echo off
:: TODO:设置MC平台环境变量
color 02
::设置java的安装路径
set javaPath=%~dp0jdk8
::设置tomcat的安装路径
set tomcatPath=%~dp0tomcat-7.0.94
:: 开始获取系统版本
if /i not "%os%"=="Windows_NT" (set TheOS=not window os&set TheBit=) else (
ver | find "4.0" > nul && set TheOS=Windows 95 && goto newOSProcess
ver | find "4.10"> nul && set TheOS=Windows 98 && goto oldOSProcess
ver | find "4.90"> nul && set TheOS=Windows me && goto oldOSProcess
ver | find "3.51"> nul && set TheOS=Windows NT35 && goto oldOSProcess
ver | find "5.0" > nul && set TheOS=Windows 2000 && goto oldOSProcess
ver | find "5.1" > nul && set TheOS=Windows XP && goto oldOSProcess
ver | find "5.2" > nul && set TheOS=Windows 2003 && goto oldOSProcess
ver | find "6.0" > nul && set TheOS=Windows Vista && goto newOSProcess
ver | find "6.1" > nul && set TheOS=Windows 7 && goto newOSProcess
ver | find "6.2" > nul && set TheOS=Windows 8 && goto newOSProcess
ver | find "10.0"> nul && set TheOS=Windows 10 && goto newOSProcess
set TheBit=x%PROCESSOR_ARCHITECTURE:~-2%
)
goto UnknownVersion
::未知操作系统处理
:UnknownVersion
echo unsupport os..
goto end
::windowsxp以前版本的os环境变量处理,通过这种设置方式貌似在window7下不会立即生效,在xp中会不会立即生效未知。
:oldOSProcess
echo your os:%TheOS% %TheBit% go to oldOSProcess
::如果有的话,先删除JAVA_HOME
wmic ENVIRONMENT where "name='JAVA_HOME'" delete
::如果有的话,先删除TOMCAT_HOME
wmic ENVIRONMENT where "name='TOMCAT_HOME'" delete
::创建JAVA_HOME
wmic ENVIRONMENT create name="JAVA_HOME",username="<system>",VariableValue="%javaPath%"
::创建TOMCAT_HOME
wmic ENVIRONMENT create name="TOMCAT_HOME",username="<system>",VariableValue="%tomcatPath%"
echo JAVA_HOME: %javaPath%
echo TOMCAT_HOME: %tomcatPath%
goto end
::windows7,Vista 以后版本执行以下处理,立刻生效。
:newOSProcess
echo your os:%TheOS% %TheBit% go to newOSProcess
setx "JAVA_HOME" "%javaPath%" -M
setx "TOMCAT_HOME" "%tomcatPath%" -M
echo JAVA_HOME: %javaPath%
echo TOMCAT_HOME: %tomcatPath%
goto end
:end
pause