- <!-- :
- @echo off
- set "c=%~1"
- set "jh=%~2"
- if defined c set n=1
- if defined jh set /a n+=1
- if "%n%" neq "2" (for /f "tokens=1,2 delims=|" %%a in ('mshta "%~f0"') do (set "c=%%a"&set "jh=%%b"))
- if not defined jh echo;未指定java_home目录&goto end
- if not exist "%jh%\bin\java.exe" echo;java_home目录错误&goto end
- echo;"%c%"|findstr "^\"[123]\"$">nul||(echo;参数错误&goto end)
- echo;JAVA_HOME="%jh%"
- goto :%c%
- :1
- wmic ENVIRONMENT where "name='JAVA_HOME'" delete 2>&1 >nul
- wmic ENVIRONMENT where "name='CLASSPATH'" delete 2>&1 >nul
- wmic ENVIRONMENT create name="JAVA_HOME",username="<system>",VariableValue="%jh%"
- wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin;%Path%"
- wmic ENVIRONMENT create name="CLASSPATH",username="<system>",VariableValue=".;%%JAVA_HOME%%\lib\tools.jar;%%JAVA_HOME%%\lib\dt.jar;%%JAVA_HOME%%\jre\lib\rt.jar"
- goto end
- :2
- set "regpath=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
- reg add "%regpath%" /v JAVA_HOME /t REG_SZ /d "%jh%" /f
- reg add "%regpath%" /v Path /t REG_EXPAND_SZ /d "%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin;%Path%" /f
- reg add "%regpath%" /v CLASSPATH /t REG_EXPAND_SZ /d ".;%%JAVA_HOME%%\lib\tools.jar;%%JAVA_HOME%%\lib\dt.jar;%%JAVA_HOME%%\jre\lib\rt.jar" /f
- goto end
- :3
- if not exist "%SystemRoot%\System32\setx.exe" echo;setx不可用&goto end
- set "pathtmp=%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin;%Path%"
- if "%pathtmp:~1023%" neq "" echo;path的字符数超过1024,setx无法正常修改&goto end
- setx JAVA_HOME "%jh%" /M
- setx Path "%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin;%Path%" /M
- setx CLASSPATH ".;%%JAVA_HOME%%\lib\tools.jar;%%JAVA_HOME%%\lib\dt.jar;%%JAVA_HOME%%\jre\lib\rt.jar" /M
- :end
- pause&exit
- -->
- <head>
- <title>设置JAVA环境变量</title>
- <HTA:APPLICATION SHOWINTASKBAR="no" MAXIMIZEBUTTON="no" SCROLL="no" />
- <script>
- window.resizeTo(400, 90);
- function getfolder(){
- var stdout=new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1)
- var Shell = new ActiveXObject("Shell.Application");
- var Folder = Shell.BrowseForFolder(0, "选择文件夹", 64, 17);
- var obj = document.getElementById("choice");
- var index = obj.selectedIndex;
- var val = obj.options[index].value;
- stdout.Write(val+"|"+(Folder==null?"":Folder.self.Path));
- close();
- }
- </script>
- </head>
- 设置方式:
- <select id="choice">
- <option value="1" selected="selected">wmic命令</option>
- <option value="2">注册表</option>
- <option value="3">setx命令</option>
- </select>
- <input type="button" value="选择JAVA_HOME的目录" onclick="getfolder();">
复制代码
|