11

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.

Jim B
  • 23,938
  • 4
  • 35
  • 58
Ricket
  • 439
  • 2
  • 7
  • 18
  • 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 03 '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 03 '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 03 '10 at 20:40
  • Here's another approach I just found: http://ivan.dretvic.com/2011/02/who-hates-java-how-to-remove-all-java-installations-on-your-network/ – Craig Ringer Nov 01 '12 at 03:29

8 Answers8

8

This line will uninstall all versions of JAVA:

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

user154791
  • 81
  • 1
  • 1
  • 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. – Michael S. Nov 06 '14 at 15:56
  • Thanks! This is best cmd for removing all old java versions. Just need to ensure it is Run with Administrator privilege. – PatricK Apr 16 '15 at 02:43
6

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
edusysadmin
  • 536
  • 2
  • 8
  • 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 05 '10 at 13:34
1

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

Paulie D
  • 11
  • 2
1

Check this simple, but very useful command:

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

stolen from here

higuita
  • 1,093
  • 9
  • 13
0

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

nedm
  • 5,610
  • 5
  • 30
  • 52
0

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 https://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
0

You can still accomplish using Batch script. I use the following script;

@ECHO OFF
REM #######################################################
REM AUTHOR: HeyVooN
REM BLOG: http://blog.teksoporte.es
REM DATE CREATED: 09/04/2015
REM Deploy Java to your Enterprise
REM This script is optimised for 32 and 64 Bit versions of Java to be installed on either 32 or 64 bit
REM operating systems. It can easily accommodate 64 bit versions of Java.
REM This script removes all previous versions of Java installed prior to (re)install any version.
REM #######################################################

REM #######################################################
REM SECTION 1 - CLEAN UP PC BEFORE INSTALLING JAVA (This section is not mandatory)
REM #######################################################
ECHO -------------------------------------------------------
ECHO STOP PROCESSES
ECHO -------------------------------------------------------
REM Assuming that processes using Java are stopped before uninstalling existing versions of Java
REM Common processes using Java: firefox.exe, iexplore.exe, chrome.exe, jusched.exe, jucheck.exe, java.exe, javacpl.exe

sc config "UI0Detect" start= disabled
sc stop UI0Detect
taskkill /F /IM jusched.exe /T
taskkill /F /IM jucheck.exe /T
taskkill /F /IM java.exe /T

ECHO -------------------------------------------------------
ECHO UNISTALL ANY PREVIOUS VERSIONS OF JAVA 32 Bit
ECHO -------------------------------------------------------
SET jver="Java 7"
SET regVar32=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
SET myCMD=REG QUERY %regVar32% /s /f %jver%
FOR /f " usebackq delims={} tokens=2" %%i IN (`%myCMD%`) DO (
 ECHO Uninstall Key: {%%i}
 ECHO Condition: %uinstallState%
 START /WAIT MSIEXEC /x {%%i} /qn /norestart
  )

ECHO -------------------------------------------------------
ECHO UNISTALL ANY PREVIOUS VERSIONS OF JAVA 64 Bit
ECHO -------------------------------------------------------
SET jver="Java 7"
SET regVar64=HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
SET myCMD=REG QUERY %regVar64% /s /f %jver%
FOR /f " usebackq delims={} tokens=2" %%i IN (`%myCMD%`) DO (
 ECHO Uninstall Key: {%%i}
 ECHO Condition: %uinstallState%
 START /WAIT MSIEXEC /x {%%i} /qn /norestart
  )

ECHO -------------------------------------------------------
ECHO CLEANING REGISTRY
ECHO -------------------------------------------------------
REM Removing known problem registry keys. I use the below line to SET the variable of which reg.exe to use depending on OS architecture.
REM Additional registry removal strings can be added here.
@If Defined ProgramFiles(x86) (SET "Reg32Path=%SystemRoot%\SysWOW64\reg.exe") Else (SET "Reg32Path=%SystemRoot%\System32\reg.exe")
REM %Reg32Path% DELETE HKLM\SOFTWARE\JavaSoft /va /f
%Reg32Path% DELETE "HKLM\SOFTWARE\JavaSoft\Auto Update" /va /f
%Reg32Path% DELETE "HKLM\SOFTWARE\JavaSoft\Java Update" /va /f

ECHO -------------------------------------------------------
ECHO DELETE START MENU ITEMS
ECHO -------------------------------------------------------
RD /s /q "%ProgramData%\Microsoft\Windows\Start Menu\Programs\Java"
PAUSE
EXIT /B 0

You can adapt/modify the way you want to suit your needs. ;-)

  • If you're a system administrator, don't run this without a warning. Force-killing Java system-wide is going to upset some people -- for example I use Eclipse every day, and some folks use OpenOffice/LibreOffice. I only mention this because my question (five years ago) was me looking for a way to silently deploy a script that would run on everyone's computer and uninstall old versions of Java. So clearly this one would not do well, or I'd remove the taskkill lines at least. – Ricket Apr 16 '15 at 02:06
0

This worked for me. It uninstalls all versions of Java (32-bit and 64-bit) prior to the latest version (which is v8.0.66 at the time of writing).

wmic product where "name like 'Java%%' and version < '8.0.66'" call uninstall
Reado
  • 692
  • 2
  • 9
  • 24