Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
out of curiosity why do you want to do this. Java version incompatability has caused me no end of pain (HP I'm looking at you). If anything I'd be tempted to write a script to install multiple versions of Java –  Jim B Sep 3 '10 at 17:04
3  
Security would be the primary concern. Outdated versions of JVM have been very popularly exploited in recent attacks. Even Windows 7 machines were impacted. –  Joshua Sep 3 '10 at 17:10
1  
Absolutely for security. I have in the last couple hours successfully deployed the latest Java across the office, but those older versions still remain. I've heard of exploits that were able to use an older version of Java that was installed but dormant in a machine, in order to install spyware, and I want to avoid any of that by ONLY having the latest Java on the computers. –  Ricket Sep 3 '10 at 20:40
    
Here's another approach I just found: ivan.dretvic.com/2011/02/… –  Craig Ringer Nov 1 '12 at 3:29

6 Answers 6

up vote 5 down vote accepted

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
share|improve this answer
    
That's great! I haven't yet cleaned up the old Java versions so I think I shall integrate this into our startup script. Thanks for sharing! –  Ricket Oct 5 '10 at 13:34

This line will uninstall all versions of JAVA:

wmic product where "name like 'Java%% %%'" call uninstall /nointeractive

share|improve this answer
    
Thank you. This works well on my system. It removed both my Java 7 as well as both my Java 8 versions with this single command. –  gentlesea Nov 6 at 15:56

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

share|improve this answer
    
"Best to read from the bottom-up" -- in that case you should copy the best script to here –  jcollum Oct 30 at 15:28

Check this simple, but very useful command:

wmic product where "name like 'Java(TM) 6%%'" call uninstall /nointeractive

stolen from here

share|improve this answer

There's some vbscript code here that you should be able to deploy via Group Policy without an msi.

share|improve this answer

The top voted verified answer has some issues with it. The 6.0 series CLSIDs are incorrect based on my testing.

Here are the CLSIDs I have found to work for each JRE series. Replace xx for the desired versions you'd like to remove. Please use with http://serverfault.com/a/187552/172014

4.2_xx
{7148F0A8-6813-11D6-A77B-00B0D0142xx0}

5.0_xx
{3248F0A8-6813-11D6-A77B-00B0D0150xx0}

6.0_xx
{26A24AE4-039D-4CA4-87B4-2F832160xxF0}

7.0_xx
{26A24AE4-039D-4CA4-87B4-2F832170xxF0}

I recommend adding /norestart after /q as well just in case the 1.4.2 series JRE MSIs want a reboot.

msiexec /x <insert_CLSID_here> /q /norestart
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.