Uninstall Old Javas (JREs and JDKs)

Please use the script to keep only latest versions of JRE and JDK

If you have many version of JRE or JDK, the script will remove older versions from the endpoint and keeps only the latest version from the inventory!

Note:
If you have the same version of latest x86 and x64 on the same machine then the script leaves x64 program and uninstall x86 program from the endpoint.

def DNDS(rtkey,pK,kA):
    import re
    ln=[]
    lv=[]
    lus=[]
    try:
        oK=_winreg.OpenKey(rtkey,pK,0,kA)
        i=0
        while True:
            try:
                bkey=_winreg.EnumKey(oK,i)
                vkey=os.path.join(pK,bkey)
                oK1=_winreg.OpenKey(rtkey,vkey,0,kA)
                try:
                    DN,bla=_winreg.QueryValueEx(oK1,'DisplayName')
                    DV,bla=_winreg.QueryValueEx(oK1,'DisplayVersion')
                    US,bla=_winreg.QueryValueEx(oK1,'UninstallString')
                    _winreg.CloseKey(oK1)
                    o=re.search(r'(^java.*|^j2se.*)update.*',DN.lower())
                    if o.group():
                        ln.append(DN)
                        lv.append(DV)
                        lus.append(US)
                except:
                    pass
                i += 1
            except:
                break
        _winreg.CloseKey(oK)
        return zip(ln,lv,lus)
    except:
        return zip(ln,lv,lus)

def ecmd(CMD):
    import ctypes
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    from subprocess import PIPE, Popen
    with disable_file_system_redirection():
        OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = OBJ.communicate()
    r=OBJ.returncode
    return r

import _winreg
import os
if 'PROGRAMFILES(X86)' in os.environ.keys():
    f=DNDS(_winreg.HKEY_LOCAL_MACHINE,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_32KEY | _winreg.KEY_READ)
    f.extend(DNDS(_winreg.HKEY_LOCAL_MACHINE,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_64KEY | _winreg.KEY_READ))
    f.extend(DNDS(_winreg.HKEY_CURRENT_USER,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_32KEY | _winreg.KEY_READ))
    f.extend(DNDS(_winreg.HKEY_CURRENT_USER,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_64KEY | _winreg.KEY_READ))
else:
    f=DNDS(_winreg.HKEY_LOCAL_MACHINE,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_READ)
    f.extend(DNDS(_winreg.HKEY_CURRENT_USER,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_READ))

if len(f)>0:
    vf=[]
    df=[]
    for i in f:
        vf.append([str(i[0]), [int(k) for k in i[1].split('.')], str(i[2])])
    vf=sorted(vf,key=lambda x:x[1],reverse=True)

    print 'Existing JREs: '
    for i in vf[:]:
        if 'Development' not in i[0]:
            print i[0], '.'.join([str(k) for k in i[1]])
        else:
            vf.remove(i)
            df.append(i)

    if len(df)>0:
        print '
'
        print 'Existing JDKs: '
        for i in df:
            print i[0], '.'.join([str(k) for k in i[1]])

    if len(df)>1:
        print '
'
        print 'Removed JDKs: '
        if len(df[2:])>0:
            for i in df[2:]:
                if ecmd(i[-1].replace('MsiExec.exe /I', 'MsiExec.exe /X')+' /qn')==0:
                    print i[0], '.'.join([str(k) for k in i[1]])
        if df[0][1]==df[1][1]:
            for i in df[:2]:
                if '(64-bit)' not in i[0]:
                    if ecmd(i[-1].replace('MsiExec.exe /I', 'MsiExec.exe /X')+' /qn')==0:
                        print i[0], '.'.join([str(k) for k in i[1]])
        else:
            if ecmd(df[1][-1].replace('MsiExec.exe /I', 'MsiExec.exe /X')+' /qn')==0:
                print df[1][0], '.'.join([str(k) for k in df[1][1]])

    if len(vf)>1:
        print '
'
        print 'Removed JREs: '
        if len(vf[2:])>0:
            for i in vf[2:]:
                if ecmd(i[-1].replace('MsiExec.exe /I', 'MsiExec.exe /X')+' /qn')==0:
                    print i[0], '.'.join([str(k) for k in i[1]])
        if vf[0][1]==vf[1][1]:
            for i in vf[:2]:
                if '(64-bit)' not in i[0]:
                    if ecmd(i[-1].replace('MsiExec.exe /I', 'MsiExec.exe /X')+' /qn')==0:
                        print i[0], '.'.join([str(k) for k in i[1]])
        else:
            if ecmd(vf[1][-1].replace('MsiExec.exe /I', 'MsiExec.exe /X')+' /qn')==0:
                print vf[1][0], '.'.join([str(k) for k in vf[1][1]])
else:
    print 'No Java Installed'

Sample Output:

Script in JSON format:

20170428-Uninstall-Old-Versions-of-Java.json (6.63 KB)

Tiny note: Typo in line 80 (“eixisting”)

Existing JREs:
Java 8 Update 121 (64-bit) 8.0.1210.13
Bonjour 3.0.0.10

Eixisting JDKs:
Java SE Development Kit 8 Update 121 (64-bit) 8.0.1210.13
Java SE Development Kit 8 Update 112 (64-bit) 8.0.1120.15
Java SE Development Kit 8 Update 111 (64-bit) 8.0.1110.14
Java SE Development Kit 8 Update 102 (64-bit) 8.0.1020.14
Java SE Development Kit 8 Update 101 (64-bit) 8.0.1010.13
Java SE Development Kit 8 Update 92 (64-bit) 8.0.920.14
Java SE Development Kit 8 Update 73 (64-bit) 8.0.730.2
Java SE Development Kit 8 Update 66 (64-bit) 8.0.660.17
Java SE Development Kit 8 Update 51 (64-bit) 8.0.510.16

Removed JDKs:
Java SE Development Kit 8 Update 51 (64-bit) 8.0.510.16

Removed JREs:
Almost working. It removed the oldest JDK but not all. Re-ran it on the same machine as System and got the following:

Existing JREs:
Java 8 Update 121 (64-bit) 8.0.1210.13
Bonjour 3.0.0.10

Eixisting JDKs:
Java SE Development Kit 8 Update 121 (64-bit) 8.0.1210.13
Java SE Development Kit 8 Update 112 (64-bit) 8.0.1120.15
Java SE Development Kit 8 Update 111 (64-bit) 8.0.1110.14
Java SE Development Kit 8 Update 102 (64-bit) 8.0.1020.14
Java SE Development Kit 8 Update 101 (64-bit) 8.0.1010.13
Java SE Development Kit 8 Update 92 (64-bit) 8.0.920.14
Java SE Development Kit 8 Update 73 (64-bit) 8.0.730.2
Java SE Development Kit 8 Update 66 (64-bit) 8.0.660.17
Java SE Development Kit 8 Update 51 (64-bit) 8.0.510.16

Removed JDKs:
Java SE Development Kit 8 Update 111 (64-bit) 8.0.1110.14
Java SE Development Kit 8 Update 102 (64-bit) 8.0.1020.14
Java SE Development Kit 8 Update 101 (64-bit) 8.0.1010.13
Java SE Development Kit 8 Update 92 (64-bit) 8.0.920.14
Java SE Development Kit 8 Update 73 (64-bit) 8.0.730.2
Java SE Development Kit 8 Update 66 (64-bit) 8.0.660.17
Java SE Development Kit 8 Update 51 (64-bit) 8.0.510.16
Java SE Development Kit 8 Update 112 (64-bit) 8.0.1120.15

Removed JREs:
Bonjour 3.0.0.10

Should “Bonjour” be getting flagged as a JRE??

Hi @PromptCare,

Sorry for the inconvenience, I will change the script flagging Bonjour as JRE and the typo mistakes.

I will update you once I have completed it.

Thank you.

Hi @PromptCare,

Please use the script now to get uninstalled the old versions of the Java

The script does not get uninstalled other than java application

The typo issue is also corrected

The updated script is placed in the same post which was published previously. Refer the link.
https://forum.mspconsortium.com/forum/script-library/10582-uninstall-old-javas-jres-and-jdks#post10582

Thank you.

Follow-up:
Just found a PC (win10 home) with multiple JRE/JDKs so manually ran the procedure:

Existing JREs:
Java 8 Update 131 8.0.1310.11
Java 8 Update 131 (64-bit) 8.0.1310.11
Java 8 Update 121 8.0.1210.13

Existing JDKs:
Java SE Development Kit 8 Update 131 (64-bit) 8.0.1310.11
Java SE Development Kit 8 Update 121 (64-bit) 8.0.1210.13
Java SE Development Kit 8 Update 92 (64-bit) 8.0.920.14

Removed JDKs:

Removed JREs:

Hi @PromptCare,

Thanks for updating the issue, We will check the script again with windows 10 home.

Hi @PromptCare,

Please use the script always as a System User, I think you have used the script as a Logged in User. Suppose if the logged in user don’t have administrator privilege then the script does not remove any program that was installed within the endpoint.

Please let me know if you have any concern on this.

Thank you.

That was probably the case. In which case, as a Feature Suggestion, could scripts get an “only run as system user” checkbox? I’ve seen some procedures where the user is predetermined – could that be put into place for scripts that we add ourselves?

Hi @PromptCare,

Is that my understanding right?

Instead of selecting ‘System User’ radio button on the ITSM, the script should run itself as system user.

If so, we will discuss the suggestion and let you know the status, until please follow the option selecting ‘System User’ through ITSM.

If not so, sorry for the misunderstanding, please let me know with some more explanation

Thank you.

I was thinking a checkbox in the area where you edit the script itself for “[ ] Always run as System” then, when you opt to run a procedure manually, it would auto-select System.

Alternatively, as some scripts must be run as the User (e.g. https://forum.mspconsortium.com/forum/script-library/4460-script-requests-comodo-will-write-the-scripts-for-you-for-free?p=10775#post10775), maybe a radio button choice on the Edit screen would be better.