Subhayan Bhattacharya
Subhayan Bhattacharya

Reputation: 5705

Understanding what java version is installed in windows machine in Powershell

I want to find out the different java versions installed in my machine and un- install a few version nos if needed.

So i tried the below two commands :

PS C:\Users\bhatsubh> java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

PS C:\Users\bhatsubh> Get-CimInstance -ClassName Win32_Product | Where-Object { $_.Vendor -match 'Oracle' -and $_.Name -match 'Java'}

Name             Caption                   Vendor                    Version                   IdentifyingNumber
----             -------                   ------                    -------                   -----------------
Java 8 Update... Java 8 Update 131         Oracle Corporation        8.0.1310.11               {26A24AE4-039D-4CA4-8...
Java 8 Update... Java 8 Update 131 (64-... Oracle Corporation        8.0.1310.11               {26A24AE4-039D-4CA4-8...
Java SE Devel... Java SE Development Ki... Oracle Corporation        8.0.1210.13               {64A3A4F4-B792-11D6-A...
Java Auto Upd... Java Auto Updater         Oracle Corporation        2.8.131.11                {4A03706F-666A-4037-7...

Now why is the version no returned by the command : java -version not listed in the Get-CimInstance command.

Can someone please help me with this? It confuses me.

Upvotes: 2

Views: 4966

Answers (4)

Niebian
Niebian

Reputation: 86

You need to know that Java SE Development Kit (JDK) and Java SE Runtime Environment (JRE) are not the same thing.

Oracle on their website says:

The JDK is a development environment for building applications, and components using the Java programming language.

If you want to run Java programs, but not develop them, download the Java Runtime Environment, or JRE™.

So, as you can see you have:

  • JRE v8.0.1310.11,
  • JRE (x64) v8.0.1310.11,
  • JDK v8.0.1210.13.

and Java Auto Updater.

java -version

depends on JRE, so it returns Java(TM) SE Runtime Environment version.

Build 1.8.0_131-b11 and 8.0.1310.11 is the same version.

Upvotes: 1

Manu
Manu

Reputation: 1742

If you have Powershell 5+ installed :

Get-Package | Where-Object { $_.Name -like "*Java*" -or $_.Name -like "*Oracle*" }

https://learn.microsoft.com/en-us/powershell/module/packagemanagement/get-package?view=powershell-5.1

Upvotes: 0

gil.fernandes
gil.fernandes

Reputation: 14601

The information about which versions are installed in Java is typically to be found in the Windows registry - and Powershell is most probably reading registry information. The best thing you can do is to inspect with regedit.exe the following keys:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JDK - (Java 9 only)

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JRE - (Java 9 only)

Below these keys you will see folders corresponding to the installed Java Development Kits and Java Runtime Environments.

The sub-keys with the version contain a key JavaHome which contains the directory where the corresponding version is installed.

Here is an example of how it should look:

enter image description here

You can read registry keys using Windows Powershell, like this:

Set-Location -Path "hklm:\SOFTWARE\JavaSoft\Java Development Kit\"
Get-ItemProperty -Path .

Upvotes: 0

tukan
tukan

Reputation: 17337

Your powershell output is truncated, please paste the whole text. If you do not have monitor large enough to fit the text redirect it via > java_out.txt

With java -version you should see what you have in your JAVA_HOME in your shell e.g. C:\Program Files\Java\jdk1.8.0_92.

The powershell queries all instances found at your computer via registry. Two different things. If you can not find your Java at the output that just means the powershell can not find it at registry via the query.

My output:

my env:  
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_92
PS C:\Users\xxx> java -version
java version "1.8.0_92"

PS C:\Users\xxx> Get-CimInstance -ClassName Win32_Product | Where-Object { $_.Vendor -match 'Oracle' -and $_.Name -match 'Java' }

Name             Caption                                     Vendor                                      Version                                     IdentifyingNumber
----             -------                                     ------                                      -------                                     -----------------
Java Configur... Java Configuration Set 1.0 EN               Oracle                                      1.0                                         {34E43632-93E0-411C-BC2F-BB6F7A86307F}
Java(TM) 6 Up... Java(TM) 6 Update 45                        Oracle                                      6.0.450                                     {26A24AE4-039D-4CA4-87B4-2F83216045FF}
Java 7 Update 40 Java 7 Update 40                            Oracle                                      7.0.400                                     {26A24AE4-039D-4CA4-87B4-2F83217040FF}
Java(TM) 6 Up... Java(TM) 6 Update 45 (64-bit)               Oracle                                      6.0.450                                     {26A24AE4-039D-4CA4-87B4-2F86416045FF}
Java 8 Update... Java 8 Update 92 (64-bit)                   Oracle Corporation                          8.0.920.14                                  {26A24AE4-039D-4CA4-87B4-2F86418092F0}
Java SE Devel... Java SE Development Kit 8 Update 92 (64-... Oracle Corporation                          8.0.920.14                                  {64A3A4F4-B792-11D6-A78A-00B0D0180920}
Java Auto Upd... Java Auto Updater                           Oracle Corporation                          2.8.92.14                                   {4A03706F-666A-4037-7777-5F2748764D10}

Notice the IdentifyingNumber column (Java Configuration Set 1.0 EN). That is the key under which you will find it in the registry.

For example you will find in registry for key: 34E43632-93E0-411C-BC2F-BB6F7A86307F the following info:

Powershell query for java

And that is what powershell sees. If your JAVA_HOME java does not have correct registry entry you won't see it in the powershell output.

Upvotes: 0

Related Questions