1
  • 我需要使用批处理脚本设置 JAVA_HOME 变量
  • 尝试了以下代码,但没有用。

for /f %%j in ("java.exe") do @echo.%%~dp$PATH:j

上面的脚本给出了空

FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Runtime Environment" /v CurrentVersion') DO set CurVer=%%B

上面的脚本给出了 1.6

目录 java.exe /B /S

上面的脚本给出了 C:\Windows\System32 但实际上 java 安装在 C:\Program Files\Java\jdk1.6.0_31

  • 我需要将 JAVA_HOME 设置为 C:\Program Files\Java\jdk1.6.0_31。

如何得到这个?

4

4 回答 4

4

我得到了解决方案。这给出了确切的安装路径。

FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Development Kit" /v CurrentVersion') DO
set CurVer=%%B ECHO %CurVer%


FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Development Kit\%CurVer%" /v JavaHome')
DO set JavaPath=%%B ECHO %JavaPath%
于 2013-07-19T06:35:35.037 回答
3

Java 1.6 的路径JavaHome

@ECHO OFF &SETLOCAL 
FOR /F "tokens=2*" %%a IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.6" /v JavaHome') DO set "JavaHome16=%%b"
ECHO %JavaHome16%

Java 1.7 的路径JavaHome

@ECHO OFF &SETLOCAL 
FOR /F "tokens=2*" %%a IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.7" /v JavaHome') DO set "JavaHome17=%%b"
ECHO %JavaHome17%
于 2013-07-19T07:00:32.823 回答
0

怎么样:

setx JAVA_HOME "path/to/it"

做得到%JAVA_HOME%

于 2013-07-19T06:02:00.397 回答
0

如果您的 JAVA_HOME 已配置,这将起作用。将其保存为 someName.bat 并运行。

echo 
   where java
pause

好的,然后使用它,通过运行这将创建一个文本文件,其中您放置此 bat 的同一位置包含 JAVA 路径

 @ECHO OFF
SET TITLE=JavaHome.bat
TITLE=%TITLE%
SETLOCAL ENABLEDELAYEDEXPANSION
SET JDKBIN=\bin
SET JREBIN=\jre\bin
SET JBINARY=%JREBIN%\java.exe
SET SCRIPTDIR=%~dp0
SET SCRIPTDIR=%SCRIPTDIR:~0,-1%
IF EXIST "%SCRIPTDIR%\javahome.txt" (
ECHO.
ECHO JDK home already set in javahome.txt file.
GOTO :LOCKFILE
)
IF DEFINED JAVA_HOME (
ECHO.
ECHO JAVA_HOME is already set to !JAVA_HOME!
CALL :STRIP "!JAVA_HOME!">"!SCRIPTDIR!\javahome.txt"
ECHO Created !SCRIPTDIR!\javahome.txt file containing JAVA_HOME
GOTO :END
)
SET "dir=%~f0"
:DIRLOOP
CALL :FGETDIR "%dir%"
IF EXIST "%dir%\%JBINARY%" (
ECHO Parent directory search found JAVA_HOME at %dir%
GOTO :SEARCHSET
)
IF "%dir:~-1%" == ":" (
ECHO Parent directory search reached root and "%JBINARY%" was not found.
GOTO :REGISTRY
)
GOTO :DIRLOOP
:SEARCHSET
SET JAVA_HOME=%dir%
ECHO %JAVA_HOME%>javahome.txt
ECHO Created file %SCRIPTDIR%\javahome.txt with value %JAVA_HOME%
GOTO :END
:REGISTRY
:: registry search section
:: runs only when JAVA_HOME not set, file search fails, and javahome.txt doesn't exist
ECHO Searching registry for JAVA_HOME...
ECHO. 2>merged.txt
ECHO. 2>list.txt
ECHO. 2>uniquelist.txt
IF NOT EXIST reg32.txt ECHO. 2>reg32.txt
IF NOT EXIST reg64.txt ECHO. 2>reg64.txt
START /w REGEDIT /e reg32.txt "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\JavaSoft\Java Development Kit"
TYPE reg32.txt | FIND "JavaHome" > merged.txt
START /w REGEDIT /e reg64.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
TYPE reg64.txt | FIND "JavaHome" >> merged.txt
FOR /f "tokens=2 delims==" %%x IN (merged.txt) DO (
CALL :STRIP "%%~x" >>list.txt
)
FOR /F "tokens=* delims= " %%a IN (list.txt) DO (
SET str=%%a
FIND /I ^"!str!^" list.txt>nul
FIND /I ^"!str!^" uniquelist.txt>nul
IF ERRORLEVEL 1 ECHO !str!>>uniquelist.txt
)
:PROMPT
ECHO Select a JDK from the list:
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%y IN (uniquelist.txt) DO (
SET /A COUNT += 1
ECHO !COUNT!: %%~y
)
SET /P NUMBER=Type a number here:
IF "%NUMBER%" GTR "%COUNT%" GOTO :PROMPT
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%z IN (uniquelist.txt) DO (
SET /A COUNT += 1
IF !COUNT!==!NUMBER! (
SET JAVA_HOME=%%~z
)
)
ECHO %JAVA_HOME%>javahome.txt
GOTO CLEANUP

:: batch functions section
:FGETDIR
SET "dir=%~dp1"
SET "dir=%dir:~0,-1%"
EXIT /B 0
:STRIP
REM Strip quotes and extra backslash from string
SET n=%~1
SET n=%n:\\=\%
SET n=%n:"=%
IF NOT "%n%"=="" ECHO %n%
GOTO :EOF
:: cleanup and end
:CLEANUP
REM cleanup of registry search
DEL /Q merged.txt
DEL /Q list.txt
DEL /Q uniquelist.txt
DEL /Q reg32.txt
DEL /Q reg64.txt
GOTO :LOCKFILE
:: if all fails
:FAILED
IF NOT DEFINED JAVA_HOME (
ECHO Error: JAVA_HOME not set in system vars, file search failed, && javahome.txt didn't exist.
GOTO :END
)
:LOCKFILE
ECHO.
SET /P JAVA_HOME=<"%SCRIPTDIR%\javahome.txt"
ECHO The file %SCRIPTDIR%\javahome.txt shows JAVA_HOME to be %JAVA_HOME%
:END
FOR /l %%a in (30,-1,1) do (TITLE %TITLE% -- Closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)

仅供参考:原始参考:https ://gist.github.com/djangofan/1445440

于 2013-07-19T06:12:40.580 回答