从批处理文件中发现Java安装在哪里?

发布于 2024-07-15 03:07:19 字数 30 浏览 13 评论 0原文

我想从批处理脚本设置 JAVA_HOME 变量

I want to set the JAVA_HOME variable from a batch script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

櫻之舞 2024-07-22 03:07:21

如果 JAVA_HOME 尚未设置,并且您想要设置它,那么您可能需要执行类似的操作,

dir java.exe /B /S

该操作将为您提供包含文件 java.exe 的所有目录的列表。 从那里您可以将该输出传送到另一个命令,该命令解析结果并选择其中一个目录,然后使用它来设置 JAVA_HOME 变量。

If JAVA_HOME isn't already set, and you want to set it, then you probably need to do something like

dir java.exe /B /S

which will give you a list of all directories containing the file java.exe. From there you can pipe that output to another command that parses the results and selects one of those directories, then uses it to set the JAVA_HOME variable.

遗失的美好 2024-07-22 03:07:21

Java 在安装时应该设置 JAVA_HOME 变量。 使用它来查找 Java 的位置:

if not "%JAVA_HOME%" == "" goto HasJavaHome

:HasJavaHome
set JAVA_HOME=... // Your new value goes here.

Java's supposed to set up the JAVA_HOME variable when it's installed. Use this to find the location of Java:

if not "%JAVA_HOME%" == "" goto HasJavaHome

:HasJavaHome
set JAVA_HOME=... // Your new value goes here.
故笙诉离歌 2024-07-22 03:07:21

这是一个方便的小批处理文件,它将搜索路径中的所有内容以查找文件的第一次出现。 神奇的一点是 for 循环中的 %%~dp$PATH:i%%i 。 %%~dp 将仅将下一个变量扩展为驱动器号和路径。 $PATH:i 在路径中搜索第一次出现的 %%i,这是传递到 for 循环的文件名。

示例:Where.bat

@echo off
setlocal

if "%1"=="" goto USAGE
set filename=%1

for %%i in (%filename%) do @echo %%~dp$PATH:i%%i

goto EOF

:USAGE

echo %0 filename

:EOF
endlocal

Here is a handy little batch file that will search everything in your path for the first occurance of a file. The magical little bit is the %%~dp$PATH:i%%i in the for loop. %%~dp will expand the next variable to the drive letter and path only. The $PATH:i searches the path for the first occurance of %%i which is the filename that is passed into the for loop.

example: Where.bat

@echo off
setlocal

if "%1"=="" goto USAGE
set filename=%1

for %%i in (%filename%) do @echo %%~dp$PATH:i%%i

goto EOF

:USAGE

echo %0 filename

:EOF
endlocal
情痴 2024-07-22 03:07:21

这不会搜索整个硬盘驱动器。 它只搜索父目录树,因此比使用“dir java.exe /B /S”要快得多。

@ECHO off
ECHO ------------------------------------------------------------------
ECHO  Define the location of an existing JDK install by 
ECHO  searching for Javasoft JDK distrobution in parent 
ECHO  directories.
ECHO.
ECHO  Script does not add trailing slash.
ECHO ------------------------------------------------------------------
IF DEFINED JAVA_HOME (
  ECHO JAVA_HOME is already set.
  GOTO :JHSET
)
SET matchobject=Javasoft\bin\java.exe
SET "dir=%~f0"
:LOOP
CALL :FGETDIR "%dir%"
IF EXIST "%dir%\%matchobject%" (
  ECHO Found JAVA_HOME at %dir%\
  GOTO :HOMESET
)
IF "%dir:~-1%" == ":" (
  ECHO Reached root and directory containing "%matchobject%" not found.
  GOTO :EOF
)
GOTO :LOOP
:HOMESET
SET JAVA_HOME=%dir%
GOTO :JHSET
:: function section
:FGETDIR
SET "dir=%~dp1"
SET "dir=%dir:~0,-1%"
EXIT /B 0
:: end function section
:JHSET
:: Does JAVA_HOME have a trailing slash? If so remove it.
IF !JAVA_HOME:~-1!==\ SET JAVA_HOME=!JAVA_HOME:~0,-1!
ECHO JAVA_HOME is %JAVA_HOME%
ECHO Will close in a few seconds...
ping -n 60 127.0.0.1>nul

This doesn't search the entire hard drive. It only searches the parent directory tree and so its much faster than using "dir java.exe /B /S".

@ECHO off
ECHO ------------------------------------------------------------------
ECHO  Define the location of an existing JDK install by 
ECHO  searching for Javasoft JDK distrobution in parent 
ECHO  directories.
ECHO.
ECHO  Script does not add trailing slash.
ECHO ------------------------------------------------------------------
IF DEFINED JAVA_HOME (
  ECHO JAVA_HOME is already set.
  GOTO :JHSET
)
SET matchobject=Javasoft\bin\java.exe
SET "dir=%~f0"
:LOOP
CALL :FGETDIR "%dir%"
IF EXIST "%dir%\%matchobject%" (
  ECHO Found JAVA_HOME at %dir%\
  GOTO :HOMESET
)
IF "%dir:~-1%" == ":" (
  ECHO Reached root and directory containing "%matchobject%" not found.
  GOTO :EOF
)
GOTO :LOOP
:HOMESET
SET JAVA_HOME=%dir%
GOTO :JHSET
:: function section
:FGETDIR
SET "dir=%~dp1"
SET "dir=%dir:~0,-1%"
EXIT /B 0
:: end function section
:JHSET
:: Does JAVA_HOME have a trailing slash? If so remove it.
IF !JAVA_HOME:~-1!==\ SET JAVA_HOME=!JAVA_HOME:~0,-1!
ECHO JAVA_HOME is %JAVA_HOME%
ECHO Will close in a few seconds...
ping -n 60 127.0.0.1>nul
梦里°也失望 2024-07-22 03:07:20

此代码片段将在当前路径中搜索 java.exe,并打印出找到它的位置:

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

在我的系统上,这给了我

C:\WINDOWS\system32\

使用此可以设置 JAVA_HOME,如下所示:

@echo off

for /f %%j in ("java.exe") do (
    set JAVA_HOME=%%~dp$PATH:j
)

if %JAVA_HOME%.==. (
    @echo java.exe not found
) else (
    @echo JAVA_HOME = %JAVA_HOME%
)

This snippet will search the current PATH for java.exe, and print out where it was found:

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

On my system this gives me

C:\WINDOWS\system32\

Using this you can set JAVA_HOME as follows:

@echo off

for /f %%j in ("java.exe") do (
    set JAVA_HOME=%%~dp$PATH:j
)

if %JAVA_HOME%.==. (
    @echo java.exe not found
) else (
    @echo JAVA_HOME = %JAVA_HOME%
)
甜扑 2024-07-22 03:07:20

此解决方案取决于安装在 %ProgramFiles%\Java 下的 JDK,例如 C:\Program Files\Java\jdk1.6.0_18。 您可以将“set JDK_Version=1.6”行更改为您要使用的版本,例如“set JDK_Version=1.5”。

假设 JDK 的最新版本位于列表底部 (jdk%jdk_Version%*),则可用的最新版本应设置为 JAVA_HOME。 如果找不到 JDK,JAVA_HOME 将不会更改。 如果找不到 JDK 并且 JAVA_HOME 没有值,脚本将显示错误消息。

@echo off
rem set the version of jdk you would like to use (1.4, 1.5, 1.6, etc)
set JDK_Version=1.6

echo.
echo Locating JDK %JDK_Version%

for /d %%i in ("%ProgramFiles%\Java\jdk%jdk_Version%*") do (set Located=%%i)
rem check if JDK was located
if "%Located%"=="" goto else
rem if JDK located display message to user
rem update %JAVA_HOME%
set JAVA_HOME=%Located%
echo     Located JDK %jdk_Version%
echo     JAVA_HOME has been set to:
echo         %JAVA_HOME%
goto endif

:else
rem if JDK was not located
rem if %JAVA_HOME% has been defined then use the existing value
echo     Could not locate JDK %JDK_Version%
if "%JAVA_HOME%"=="" goto NoExistingJavaHome
echo     Existing value of JAVA_HOME will be used:
echo         %JAVA_HOME%
goto endif

:NoExistingJavaHome
rem display message to the user that %JAVA_HOME% is not available
echo     No Existing value of JAVA_HOME is available
goto endif

:endif
rem clear the variables used by this script
set JDK_Version=
set Located=

This solution depends on the JDK being installed under %ProgramFiles%\Java, for example C:\Program Files\Java\jdk1.6.0_18. You can change the line "set JDK_Version=1.6" to the version you want to use such as "set JDK_Version=1.5".

Assuming that the latest version of JDK would be at the bottom of the list (jdk%jdk_Version%*) the latest version available should be set as JAVA_HOME. If the JDK could not be found JAVA_HOME will not be changed. If the JDK could not be found and JAVA_HOME doesn't have a value the script will display an error message.

@echo off
rem set the version of jdk you would like to use (1.4, 1.5, 1.6, etc)
set JDK_Version=1.6

echo.
echo Locating JDK %JDK_Version%

for /d %%i in ("%ProgramFiles%\Java\jdk%jdk_Version%*") do (set Located=%%i)
rem check if JDK was located
if "%Located%"=="" goto else
rem if JDK located display message to user
rem update %JAVA_HOME%
set JAVA_HOME=%Located%
echo     Located JDK %jdk_Version%
echo     JAVA_HOME has been set to:
echo         %JAVA_HOME%
goto endif

:else
rem if JDK was not located
rem if %JAVA_HOME% has been defined then use the existing value
echo     Could not locate JDK %JDK_Version%
if "%JAVA_HOME%"=="" goto NoExistingJavaHome
echo     Existing value of JAVA_HOME will be used:
echo         %JAVA_HOME%
goto endif

:NoExistingJavaHome
rem display message to the user that %JAVA_HOME% is not available
echo     No Existing value of JAVA_HOME is available
goto endif

:endif
rem clear the variables used by this script
set JDK_Version=
set Located=
薄荷梦 2024-07-22 03:07:20

您可以使用注册表中存储的值自动发现 Java 的安装位置并设置 JAVA_HOME 变量。

HKLM > Software > JavaSoft > Java Runtime Environment

在此位置有一个名为 CurrentVersion 的键。 此版本按名称引用此级别的目录之一。 打开该目录会公开另一个名为 JavaHome 的键。 JavaHome 的值是一个文件系统路径,可用于定义环境变量 JAVA_HOME。

在批处理文件中,您可以执行以下操作:

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

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

如果您想了解更多信息,我已经编写了 教程 描述构建批处理文件以自动发现 JAVA_HOME 所需的内容。

You can use values stored in the registry to automatically discover where Java is installed and setup the JAVA_HOME variable.

HKLM > Software > JavaSoft > Java Runtime Environment

At this location is a Key called CurrentVersion. This version references one of the directories at this level by name. Opening the directory exposes another key called JavaHome. The value of JavaHome is a file system path that can be used to define your environment variable JAVA_HOME.

Within a batch file you can do something like:

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

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

If you want to read more, I've written a tutorial describing what is needed to construct a batch file to auto-discover JAVA_HOME.

究竟谁懂我的在乎 2024-07-22 03:07:20

虽然不完美,但您可以使用以下命令来发现当前的 JRE 文件夹:

for /d %%i in ("\Program Files\Java\jre*") do set JAVA_HOME=%%i

如果它是您想要的 JDK,请使用:

for /d %%i in ("\Program Files\Java\jdk*") do set JAVA_HOME=%%i`

对于基于检查 Windows 注册表的更强大的解决方案,请使用:

set KeyName=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
set Cmd=reg query "%KeyName%" /s
for /f "tokens=2*" %%i in ('%Cmd% ^| find "JavaHome"') do set JAVA_HOME=%%j

对于 JDK,您将需要此行:

set KeyName=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit

While not perfect, you can use the following to discover the current JRE folder:

for /d %%i in ("\Program Files\Java\jre*") do set JAVA_HOME=%%i

If it's the JDK you want, use:

for /d %%i in ("\Program Files\Java\jdk*") do set JAVA_HOME=%%i`

For a more robust solution based on checking the Windows Registry, use:

set KeyName=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
set Cmd=reg query "%KeyName%" /s
for /f "tokens=2*" %%i in ('%Cmd% ^| find "JavaHome"') do set JAVA_HOME=%%j

For the JDK, you'll need this line instead:

set KeyName=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文