如何在windows机器上找到JDK的安装位置?

时间:2021-01-16 07:10:40

I need to know where JDK is located on my machine.

我需要知道JDK在我的机器上的位置。

On running Java -version in cmd, it shows the version as '1.6.xx'. To find the location of this SDK on my machine I tried using echo %JAVA_HOME% but it is only showing 'JAVA_HOME' (as there is no 'JAVA_PATH' var set in my environment variables).

在cmd中运行Java -version时,它将版本显示为“1.6.xx”。为了在我的机器上找到这个SDK的位置,我尝试使用echo %JAVA_HOME%,但是它只显示'JAVA_HOME'(因为我的环境变量中没有'JAVA_PATH' var集)。

21 个解决方案

#1


244  

If you are using Linux/Unix/Mac OS X:

如果您正在使用Linux/Unix/Mac OS X:

Try this:

试试这个:

$ which java

Should output the exact location.

应该输出确切的位置。

After that, you can set JAVA_HOME environment variable yourself.

之后,您可以自己设置JAVA_HOME环境变量。

In my computer (Mac OS X - Snow Leopard):

在我的电脑(Mac OS X - Snow Leopard):

$ which java
/usr/bin/java
$ ls -l /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 Nov  7 07:59 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

If you are using Windows:

如果您正在使用Windows:

c:\> for %i in (java.exe) do @echo.   %~$PATH:i

#2


92  

Windows > Start > cmd >

Windows >启动> cmd >

C:> for %i in (javac.exe) do @echo.   %~$PATH:i

If you have a JDK installed, the Path is displayed,
for example: C:\Program Files\Java\jdk1.6.0_30\bin\javac.exe

如果你有一个JDK安装,显示的路径,例如:C:\Program Files\Java\ jdk1.6.0_30 \ bin \ javac.exe

#3


72  

In Windows at the command prompt

在命令提示符的窗口中

where javac

在javac

#4


26  

Java installer puts several files into %WinDir%\System32 folder (java.exe, javaws.exe and some others). When you type java.exe in command line or create process without full path, Windows runs these as last resort if they are missing in %PATH% folders.

Java安装程序将几个文件放入%WinDir%\System32文件夹(Java)。exe,javaw。exe和其他一些)。当你的java类型。在命令行中的exe或创建没有完整路径的进程,如果它们在% path %文件夹中丢失,Windows将作为最后的选择。

You can lookup all versions of Java installed in registry. Take a look at HKLM\SOFTWARE\JavaSoft\Java Runtime Environment and HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment for 32-bit java on 64 bit Windows.

您可以查找注册表中安装的所有Java版本。看看HKLM\软件\JavaSoft\Java运行时环境和HKLM\软件\Wow6432Node\JavaSoft\Java运行时环境对于64位Windows上的32位Java。

This is how java itself finds out different versions installed. And this is why both 32-bit and 64-bit version can co-exist and works fine without interfering.

这就是java本身发现安装了不同版本的原因。这就是为什么32位和64位版本都可以共存,并且可以在不受干扰的情况下正常工作。

#5


19  

In windows the default is: C:\Program Files\Java\jdk1.6.0_14 (where the numbers may differ, as they're the version).

在windows默认是:C:\Program Files\Java\ jdk1.6.0_14(数字可能有所不同,就像他们的版本)。

#6


17  

Plain and simple on Windows platforms:

在Windows平台上简单明了:

where java

在java

#7


14  

The batch script below will print out the existing default JRE. It can be easily modified to find the JDK version installed by replacing the Java Runtime Environment with Java Development Kit.

下面的批处理脚本将打印出现有的默认JRE。通过用Java开发工具包替换Java运行时环境,可以很容易地修改它以找到已安装的JDK版本。

@echo off

setlocal

::- Get the Java Version
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
set VALUE=CurrentVersion
reg query %KEY% /v %VALUE% 2>nul || (
    echo JRE not installed 
    exit /b 1
)
set JRE_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JRE_VERSION=%%b
)

echo JRE VERSION: %JRE_VERSION%

::- Get the JavaHome
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%JRE_VERSION%"
set VALUE=JavaHome
reg query %KEY% /v %VALUE% 2>nul || (
    echo JavaHome not installed
    exit /b 1
)

set JAVAHOME=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JAVAHOME=%%b
)

echo JavaHome: %JAVAHOME%

endlocal

#8


10  

More on Windows... variable java.home is not always the same location as the binary that is run.

Windows上的更多……java变量。home并不总是与运行的二进制文件相同的位置。

As Denis The Menace says, the installer puts Java files into Program Files, but also java.exe into System32. With nothing Java related on the path java -version can still work. However when PeterMmm's program is run it reports the value of Program Files as java.home, this is not wrong (Java is installed there) but the actual binary being run is located in System32.

正如威胁者Denis The threat所说,安装程序将Java文件放入程序文件中,但也包括Java文件。exe System32系统。在不涉及Java的情况下,Java版本仍然可以工作。然而,当PeterMmm的程序运行时,它会报告程序文件的java值。在家里,这没有错(Java安装在那里),但是实际运行的二进制文件位于System32中。

One way to hunt down the location of the java.exe binary, add the following line to PeterMmm's code to keep the program running a while longer:

一种寻找java位置的方法。exe二进制,在PeterMmm代码中添加如下一行,使程序运行时间更长:

try{Thread.sleep(60000);}catch(Exception e) {}

Compile and run it, then hunt down the location of the java.exe image. E.g. in Windows 7 open the task manager, find the java.exe entry, right click and select 'open file location', this opens the exact location of the Java binary. In this case it would be System32.

编译并运行它,然后查找java的位置。exe形象。在Windows 7中打开任务管理器,找到java。exe条目,右键单击并选择“打开文件位置”,这将打开Java二进制文件的确切位置。这里是系统32。

#9


9  

Run this program from commandline:

从命令行运行这个程序:

// File: Main.java
public class Main {

    public static void main(String[] args) {
       System.out.println(System.getProperty("java.home"));
    }

}


$ javac Main.java
$ java Main

#10


9  

In a Windows command prompt, just type:

在Windows命令提示符中,只需键入:

set java_home

Or, if you don't like the command environment, you can check it from:

或者,如果您不喜欢命令环境,您可以从以下方面进行检查:

Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and in system variable try to find JAVA_HOME.

启动菜单>计算机>系统性能>先进系统性能。然后打开Advanced tab >环境变量,并在系统变量中找到JAVA_HOME。

如何在windows机器上找到JDK的安装位置?

#11


8  

Under Windows, you can use

在Windows下,你可以使用

C:>dir /b /s java.exe

to print the full path of each and every "java.exe" on your C: drive, regardless of whether they are on your PATH environment variable.

打印每个“java”的完整路径。在你的C:驱动器上的exe,不管它们是否在你的路径环境变量上。

#12


5  

Have you tried looking at your %PATH% variable. That's what Windows uses to find any executable.

您是否尝试过查看您的%PATH%变量。这就是Windows用来查找任何可执行文件的方法。

#13


5  

Just execute the set command in your command line. Then you see all the environments variables you have set.

只需在命令行中执行set命令。然后你会看到你设置的所有环境变量。

Or if on Unix you can simplify it:

或者在Unix上你可以简化它:

$ set | grep "JAVA_HOME" 

#14


5  

On Windows 10 you can find out the path by going to Control Panel > Java. In the panel that shows up, you can find the path as demonstrated in the screenshot below. In the Java Control Panel, go to the 'Java' tab and then click the 'View' button under the description 'View and manage Java Runtime versions and settings for Java applications and applets.'

在Windows 10上,你可以通过控制面板> Java找到路径。在显示的面板中,您可以找到如下屏幕截图所示的路径。在Java控制面板中,转到“Java”选项卡,然后单击description 'View'下的“View”按钮,并管理Java应用程序和applet的Java运行时版本和设置。

This should work on Windows 7 and possibly other recent versions of Windows.

这应该适用于Windows 7,也可能适用于Windows的其他最新版本。

如何在windows机器上找到JDK的安装位置?

#15


4  

Powershell one liner:

Powershell衬:

$p='HKLM:\SOFTWARE\JavaSoft\Java Development Kit'; $v=(gp $p).CurrentVersion; (gp $p/$v).JavaHome

#16


3  

This is OS specific. On Unix:

这是操作系统特定的。在Unix:

which java

will display the path to the executable. I don't know of a Windows equivalent, but there you typically have the bin folder of the JDK installation in the system PATH:

将显示可执行文件的路径。我不知道Windows是否等效,但是在这里,您通常会在系统路径中有JDK安装的bin文件夹:

echo %PATH%

#17


2  

None of these answers are correct for Linux if you are looking for the home that includes the subdirs such as: bin, docs, include, jre, lib, etc.

如果您正在寻找包含子目录(如:bin、docs、include、jre、lib等)的home,那么这些答案对于Linux都是不正确的。

On Ubuntu for openjdk1.8.0, this is in: /usr/lib/jvm/java-1.8.0-openjdk-amd64

在openjdk1.8.0的Ubuntu中,这是:/usr/lib/jvm/java-1.8.0-openjdk-amd64

and you may prefer to use that for JAVA_HOME since you will be able to find headers if you build JNI source files. While it's true which java will provide the binary path, it is not the true JDK home.

您可能更喜欢将其用于JAVA_HOME,因为如果构建JNI源文件,您将能够找到头文件。虽然java将提供二进制路径,但它不是真正的JDK主页。

#18


1  

On macOS, run:

在macOS,运行:

cd /tmp && echo 'public class Main {public static void main(String[] args) {System.out.println(System.getProperty("java.home"));}}' > Main.java && javac Main.java && java Main

On my machine, this prints:

在我的机器上,这个打印:

/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home

Note that running which java does not show the JDK location, because the java command is instead part of JavaVM.framework, which wraps the real JDK:

注意,运行哪个java不显示JDK位置,因为java命令是java .framework的一部分,它封装了真正的JDK:

$ which java
/usr/bin/java
/private/tmp
$ ls -l /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 14 Nov 17:37 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

#19


0  

in Windows cmd:

在Windows cmd:

set "JAVA_HOME" 

#20


0  

#!/bin/bash

if [[ $(which ${JAVA_HOME}/bin/java) ]]; then
    exe="${JAVA_HOME}/bin/java"
elif [[ $(which java) ]]; then
    exe="java"
else 
    echo "Java environment is not detected."
    exit 1
fi

${exe} -version

For windows:

windows:

@echo off
if "%JAVA_HOME%" == "" goto nojavahome

echo Using JAVA_HOME            :   %JAVA_HOME%

"%JAVA_HOME%/bin/java.exe" -version
goto exit

:nojavahome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program.
goto exit

:exit

This link might help to explain how to find java executable from bash: http://srcode.org/2014/05/07/detect-java-executable/

这个链接可能有助于解释如何从bash: http://srcode.org/2014/05/07/dete-java-executable/找到java可执行文件

#21


0  

I have improved munsingh's answer above by testing for the registry key in 64-bit and 32-bit registries, if needed:

如果需要,我在64位和32位注册中心中测试注册表键,从而改进了上面的munsingh的答案:

::- Test for the registry location  
SET VALUE=CurrentVersion
SET KEY_1="HKLM\SOFTWARE\JavaSoft\Java Development Kit"
SET KEY_2=HKLM\SOFTWARE\JavaSoft\JDK
SET REG_1=reg.exe
SET REG_2="C:\Windows\sysnative\reg.exe"
SET REG_3="C:\Windows\syswow64\reg.exe"

SET KEY=%KEY_1%
SET REG=%REG_1%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_1%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

::- %REG_2% is for 64-bit installations, using "C:\Windows\sysnative"
SET KEY=%KEY_1%
SET REG=%REG_2%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_2%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

::- %REG_3% is for 32-bit installations on a 64-bit system, using "C:\Windows\syswow64"
SET KEY=%KEY_1%
SET REG=%REG_3%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_3%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

:_set_value
FOR /F "tokens=2,*" %%a IN ('%REG% QUERY %KEY% /v %VALUE%') DO (
    SET JDK_VERSION=%%b
)
SET KEY=%KEY%\%JDK_VERSION%
SET VALUE=JavaHome
FOR /F "tokens=2,*" %%a IN ('%REG% QUERY %KEY% /v %VALUE%') DO (
    SET JAVAHOME=%%b
)
ECHO "%JAVAHOME%"
::- SETX JAVA_HOME "%JAVAHOME%"

reference for access to the 64-bit registry

对访问64位注册表的引用。

#1


244  

If you are using Linux/Unix/Mac OS X:

如果您正在使用Linux/Unix/Mac OS X:

Try this:

试试这个:

$ which java

Should output the exact location.

应该输出确切的位置。

After that, you can set JAVA_HOME environment variable yourself.

之后,您可以自己设置JAVA_HOME环境变量。

In my computer (Mac OS X - Snow Leopard):

在我的电脑(Mac OS X - Snow Leopard):

$ which java
/usr/bin/java
$ ls -l /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 Nov  7 07:59 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

If you are using Windows:

如果您正在使用Windows:

c:\> for %i in (java.exe) do @echo.   %~$PATH:i

#2


92  

Windows > Start > cmd >

Windows >启动> cmd >

C:> for %i in (javac.exe) do @echo.   %~$PATH:i

If you have a JDK installed, the Path is displayed,
for example: C:\Program Files\Java\jdk1.6.0_30\bin\javac.exe

如果你有一个JDK安装,显示的路径,例如:C:\Program Files\Java\ jdk1.6.0_30 \ bin \ javac.exe

#3


72  

In Windows at the command prompt

在命令提示符的窗口中

where javac

在javac

#4


26  

Java installer puts several files into %WinDir%\System32 folder (java.exe, javaws.exe and some others). When you type java.exe in command line or create process without full path, Windows runs these as last resort if they are missing in %PATH% folders.

Java安装程序将几个文件放入%WinDir%\System32文件夹(Java)。exe,javaw。exe和其他一些)。当你的java类型。在命令行中的exe或创建没有完整路径的进程,如果它们在% path %文件夹中丢失,Windows将作为最后的选择。

You can lookup all versions of Java installed in registry. Take a look at HKLM\SOFTWARE\JavaSoft\Java Runtime Environment and HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment for 32-bit java on 64 bit Windows.

您可以查找注册表中安装的所有Java版本。看看HKLM\软件\JavaSoft\Java运行时环境和HKLM\软件\Wow6432Node\JavaSoft\Java运行时环境对于64位Windows上的32位Java。

This is how java itself finds out different versions installed. And this is why both 32-bit and 64-bit version can co-exist and works fine without interfering.

这就是java本身发现安装了不同版本的原因。这就是为什么32位和64位版本都可以共存,并且可以在不受干扰的情况下正常工作。

#5


19  

In windows the default is: C:\Program Files\Java\jdk1.6.0_14 (where the numbers may differ, as they're the version).

在windows默认是:C:\Program Files\Java\ jdk1.6.0_14(数字可能有所不同,就像他们的版本)。

#6


17  

Plain and simple on Windows platforms:

在Windows平台上简单明了:

where java

在java

#7


14  

The batch script below will print out the existing default JRE. It can be easily modified to find the JDK version installed by replacing the Java Runtime Environment with Java Development Kit.

下面的批处理脚本将打印出现有的默认JRE。通过用Java开发工具包替换Java运行时环境,可以很容易地修改它以找到已安装的JDK版本。

@echo off

setlocal

::- Get the Java Version
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
set VALUE=CurrentVersion
reg query %KEY% /v %VALUE% 2>nul || (
    echo JRE not installed 
    exit /b 1
)
set JRE_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JRE_VERSION=%%b
)

echo JRE VERSION: %JRE_VERSION%

::- Get the JavaHome
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%JRE_VERSION%"
set VALUE=JavaHome
reg query %KEY% /v %VALUE% 2>nul || (
    echo JavaHome not installed
    exit /b 1
)

set JAVAHOME=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JAVAHOME=%%b
)

echo JavaHome: %JAVAHOME%

endlocal

#8


10  

More on Windows... variable java.home is not always the same location as the binary that is run.

Windows上的更多……java变量。home并不总是与运行的二进制文件相同的位置。

As Denis The Menace says, the installer puts Java files into Program Files, but also java.exe into System32. With nothing Java related on the path java -version can still work. However when PeterMmm's program is run it reports the value of Program Files as java.home, this is not wrong (Java is installed there) but the actual binary being run is located in System32.

正如威胁者Denis The threat所说,安装程序将Java文件放入程序文件中,但也包括Java文件。exe System32系统。在不涉及Java的情况下,Java版本仍然可以工作。然而,当PeterMmm的程序运行时,它会报告程序文件的java值。在家里,这没有错(Java安装在那里),但是实际运行的二进制文件位于System32中。

One way to hunt down the location of the java.exe binary, add the following line to PeterMmm's code to keep the program running a while longer:

一种寻找java位置的方法。exe二进制,在PeterMmm代码中添加如下一行,使程序运行时间更长:

try{Thread.sleep(60000);}catch(Exception e) {}

Compile and run it, then hunt down the location of the java.exe image. E.g. in Windows 7 open the task manager, find the java.exe entry, right click and select 'open file location', this opens the exact location of the Java binary. In this case it would be System32.

编译并运行它,然后查找java的位置。exe形象。在Windows 7中打开任务管理器,找到java。exe条目,右键单击并选择“打开文件位置”,这将打开Java二进制文件的确切位置。这里是系统32。

#9


9  

Run this program from commandline:

从命令行运行这个程序:

// File: Main.java
public class Main {

    public static void main(String[] args) {
       System.out.println(System.getProperty("java.home"));
    }

}


$ javac Main.java
$ java Main

#10


9  

In a Windows command prompt, just type:

在Windows命令提示符中,只需键入:

set java_home

Or, if you don't like the command environment, you can check it from:

或者,如果您不喜欢命令环境,您可以从以下方面进行检查:

Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and in system variable try to find JAVA_HOME.

启动菜单>计算机>系统性能>先进系统性能。然后打开Advanced tab >环境变量,并在系统变量中找到JAVA_HOME。

如何在windows机器上找到JDK的安装位置?

#11


8  

Under Windows, you can use

在Windows下,你可以使用

C:>dir /b /s java.exe

to print the full path of each and every "java.exe" on your C: drive, regardless of whether they are on your PATH environment variable.

打印每个“java”的完整路径。在你的C:驱动器上的exe,不管它们是否在你的路径环境变量上。

#12


5  

Have you tried looking at your %PATH% variable. That's what Windows uses to find any executable.

您是否尝试过查看您的%PATH%变量。这就是Windows用来查找任何可执行文件的方法。

#13


5  

Just execute the set command in your command line. Then you see all the environments variables you have set.

只需在命令行中执行set命令。然后你会看到你设置的所有环境变量。

Or if on Unix you can simplify it:

或者在Unix上你可以简化它:

$ set | grep "JAVA_HOME" 

#14


5  

On Windows 10 you can find out the path by going to Control Panel > Java. In the panel that shows up, you can find the path as demonstrated in the screenshot below. In the Java Control Panel, go to the 'Java' tab and then click the 'View' button under the description 'View and manage Java Runtime versions and settings for Java applications and applets.'

在Windows 10上,你可以通过控制面板> Java找到路径。在显示的面板中,您可以找到如下屏幕截图所示的路径。在Java控制面板中,转到“Java”选项卡,然后单击description 'View'下的“View”按钮,并管理Java应用程序和applet的Java运行时版本和设置。

This should work on Windows 7 and possibly other recent versions of Windows.

这应该适用于Windows 7,也可能适用于Windows的其他最新版本。

如何在windows机器上找到JDK的安装位置?

#15


4  

Powershell one liner:

Powershell衬:

$p='HKLM:\SOFTWARE\JavaSoft\Java Development Kit'; $v=(gp $p).CurrentVersion; (gp $p/$v).JavaHome

#16


3  

This is OS specific. On Unix:

这是操作系统特定的。在Unix:

which java

will display the path to the executable. I don't know of a Windows equivalent, but there you typically have the bin folder of the JDK installation in the system PATH:

将显示可执行文件的路径。我不知道Windows是否等效,但是在这里,您通常会在系统路径中有JDK安装的bin文件夹:

echo %PATH%

#17


2  

None of these answers are correct for Linux if you are looking for the home that includes the subdirs such as: bin, docs, include, jre, lib, etc.

如果您正在寻找包含子目录(如:bin、docs、include、jre、lib等)的home,那么这些答案对于Linux都是不正确的。

On Ubuntu for openjdk1.8.0, this is in: /usr/lib/jvm/java-1.8.0-openjdk-amd64

在openjdk1.8.0的Ubuntu中,这是:/usr/lib/jvm/java-1.8.0-openjdk-amd64

and you may prefer to use that for JAVA_HOME since you will be able to find headers if you build JNI source files. While it's true which java will provide the binary path, it is not the true JDK home.

您可能更喜欢将其用于JAVA_HOME,因为如果构建JNI源文件,您将能够找到头文件。虽然java将提供二进制路径,但它不是真正的JDK主页。

#18


1  

On macOS, run:

在macOS,运行:

cd /tmp && echo 'public class Main {public static void main(String[] args) {System.out.println(System.getProperty("java.home"));}}' > Main.java && javac Main.java && java Main

On my machine, this prints:

在我的机器上,这个打印:

/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home

Note that running which java does not show the JDK location, because the java command is instead part of JavaVM.framework, which wraps the real JDK:

注意,运行哪个java不显示JDK位置,因为java命令是java .framework的一部分,它封装了真正的JDK:

$ which java
/usr/bin/java
/private/tmp
$ ls -l /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 14 Nov 17:37 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

#19


0  

in Windows cmd:

在Windows cmd:

set "JAVA_HOME" 

#20


0  

#!/bin/bash

if [[ $(which ${JAVA_HOME}/bin/java) ]]; then
    exe="${JAVA_HOME}/bin/java"
elif [[ $(which java) ]]; then
    exe="java"
else 
    echo "Java environment is not detected."
    exit 1
fi

${exe} -version

For windows:

windows:

@echo off
if "%JAVA_HOME%" == "" goto nojavahome

echo Using JAVA_HOME            :   %JAVA_HOME%

"%JAVA_HOME%/bin/java.exe" -version
goto exit

:nojavahome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program.
goto exit

:exit

This link might help to explain how to find java executable from bash: http://srcode.org/2014/05/07/detect-java-executable/

这个链接可能有助于解释如何从bash: http://srcode.org/2014/05/07/dete-java-executable/找到java可执行文件

#21


0  

I have improved munsingh's answer above by testing for the registry key in 64-bit and 32-bit registries, if needed:

如果需要,我在64位和32位注册中心中测试注册表键,从而改进了上面的munsingh的答案:

::- Test for the registry location  
SET VALUE=CurrentVersion
SET KEY_1="HKLM\SOFTWARE\JavaSoft\Java Development Kit"
SET KEY_2=HKLM\SOFTWARE\JavaSoft\JDK
SET REG_1=reg.exe
SET REG_2="C:\Windows\sysnative\reg.exe"
SET REG_3="C:\Windows\syswow64\reg.exe"

SET KEY=%KEY_1%
SET REG=%REG_1%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_1%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

::- %REG_2% is for 64-bit installations, using "C:\Windows\sysnative"
SET KEY=%KEY_1%
SET REG=%REG_2%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_2%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

::- %REG_3% is for 32-bit installations on a 64-bit system, using "C:\Windows\syswow64"
SET KEY=%KEY_1%
SET REG=%REG_3%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_3%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

:_set_value
FOR /F "tokens=2,*" %%a IN ('%REG% QUERY %KEY% /v %VALUE%') DO (
    SET JDK_VERSION=%%b
)
SET KEY=%KEY%\%JDK_VERSION%
SET VALUE=JavaHome
FOR /F "tokens=2,*" %%a IN ('%REG% QUERY %KEY% /v %VALUE%') DO (
    SET JAVAHOME=%%b
)
ECHO "%JAVAHOME%"
::- SETX JAVA_HOME "%JAVAHOME%"

reference for access to the 64-bit registry

对访问64位注册表的引用。