Batch file to uninstall all Sun Java versions?
I'm setting up a system to keep Java in our office up to date. Everyone has all different versions of Java, many of them old and insecure, and some dating back as far as 1.4. I have a System Center Essentials server which can push out and silently run a .msi file, and I've already tested that it can install the latest Java. But old versions (such as 1.4) aren't removed by the installer, so I need to uninstall them. Everyone is running Windows XP.
The neat coincidence is that Sun just got bought by Oracle and Oracle has now changed all the instances of "Sun" to "Oracle" in Java. So, I can conveniently not have to worry about uninstalling the latest Java, because I can just do a search and uninstall all Sun Java programs.
I found the following batch script on a forum post which looked promising:
@echo off & cls
Rem List all Installation subkeys from uninstall key.
echo Searching Registry for Java Installs
for /f %%I in ('reg query HKLM\SOFTWARE\microsoft\windows\currentversion\uninstall') do echo %%I | find "{" > nul && call :All-Installations %%I
echo Search Complete..
goto :EOF
:All-Installations
Rem Filter out all but the Sun Installations
for /f "tokens=2*" %%T in ('reg query %1 /v Publisher 2^> nul') do echo %%U | find "Sun" > nul && call :Sun-Installations %1
goto :EOF
:Sun-Installations
Rem Filter out all but the Sun-Java Installations. Note the tilda + n, which drops all the subkeys from the path
for /f "tokens=2*" %%T in ('reg query %1 /v DisplayName 2^> nul') do echo . Uninstalling - %%U: | find "Java" && call :Sun-Java-Installs %~n1
goto :EOF
:Sun-Java-Installs
Rem Run Uninstaller for the installation
MsiExec.exe /x%1 /qb
echo . Uninstall Complete, Resuming Search..
goto :EOF
However, when I run the script, I get the following output:
Searching Registry for Java Installs
'DEV_24x6' is not recognized as an internal or external command,
operable program or batch file.
'SUBSYS_542214F1' is not recognized as an internal or external command,
operable program or batch file.
And then it appears to hang and I ctrl-c to stop it.
Reading through the script, I don't understand everything, but I don't know why it is trying to run pieces of registry keys as programs. What is wrong with the batch script? How can I fix it, so that I can move on to somehow turning it into a MSI and deploying it to everyone to clean up this office?
Or alternatively, can you suggest a better solution or existing MSI file to do what I need? I just want to make sure to get all the old versions of Java off of everyone's computers, since I've heard of exploits that cause web pages to load using old versions of Java and I want to avoid those.
Solution 1:
The following is what we use, works great as a startup script:
on error resume next
dim WshShell
dim RegKey
dim ScriptVerKey
Set WshShell = CreateObject("WScript.Shell")
'checks to see if registry key exists, if it does, the cleanup runs.
RegKey = "HKLM\SOFTWARE\EDU\Cleanup\"
ScriptVerKey = WshShell.RegRead(RegKey & "JAVAOldVer")
if ScriptVerKey <> "1" Then
Cleanup()
Else
WScript.Quit
End If
Sub Cleanup()
' Uninstall Java 1.4.1.01
WshShell.Run "msiexec /x {1666FA7C-CB5F-11D6-A78C-00B0D079AF64} /q",1,True
' Uninstall Java 1.4.2.04
WshShell.Run "msiexec /x {7148F0A8-6813-11D6-A77B-00B0D0142040} /q",1,True
' Uninstall Java 5.0.2
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150020} /q",1,True
' Uninstall Java 5.0.4
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150040} /q",1,True
' Uninstall Java 5.0.5
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150050} /q",1,True
' Uninstall Java 5.0.6
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150060} /q",1,True
' Uninstall Java 5.0.7
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150070} /q",1,True
' Uninstall Java 5.0.10
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150100} /q",1,True
' Uninstall Java 5.0.11
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150110} /q",1,True
' Uninstall Java 5.0.12
'WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0150120} /q",1,True
' Uninstall Java 6.0.0
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0160000} /q",1,True
' Uninstall Java 6.0.1
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0160010} /q",1,True
' Uninstall Java 6.0.2
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0160020} /q",1,True
' Uninstall Java 6.0.5
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0160050} /q",1,True
' Uninstall Java 6.0.30 (020608)
WshShell.Run "msiexec /x {3248F0A8-6813-11D6-A77B-00B0D0160030} /q",1,True
'regkey below stops the script from running again
WshShell.RegWrite "HKLM\Software\EDU\", "Default"
WshShell.RegWrite "HKLM\Software\EDU\Cleanup\", "Default"
WshShell.RegWrite "HKLM\Software\EDU\Cleanup\JAVAOldVer", 1, "REG_SZ"
End Sub
Solution 2:
This line will uninstall all versions of JAVA:
wmic product where "name like 'Java%% %%'" call uninstall /nointeractive
Solution 3:
Excerpt from: http://www.java.com/en/download/faq/remove_olderversions.xml
Over time, you may have installed multiple versions of Java to run available Java content. In the past, each Java update was installed in separate directories on your system. However, Java updates are now installed in a single directory.
Should I remove older versions of Java? We highly recommend users remove all older versions of Java from your system. Keeping old and unsupported versions of Java on your system presents a serious security risk. Removing older versions of Java from your system ensures that Java applications will run with the most up-to-date security and performance improvements on your system.
I found the following VBScript which, in theory, properly removes prior versions of Java. Best to read from the bottom-up, since others have tweaked it to perfection:
http://www.appdeploy.com/messageboards/tm.asp?m=29809
Solution 4:
Check this simple, but very useful command:
wmic product where "name like 'Java(TM) 6%%'" call uninstall /nointeractive
stolen from here