The following script can be used to uninstall all versions of Java on a computer.
 
Please note:

  • Script was found here: AppDeploy / ITNinja
  • Original article was found here: Mockbox
  • Some versions of Java cannot be uninstalled silently (e.g. Version 6 Update 20)
  • Script needs to be ran from the command line (usage examples below)

 
WARNING:
Uninstalling Java can be troublesome. Some versions do not want to be uninstalled silently (e.g. by a script or command) and some may half uninstall resulting in a somewhat impaired computer. I strongly suggest you thoroughly test this script in your environment before deploying to production computers.
 

How do I use the script?

[divider style=”thin”]
 
Download JavaUninstallScript.vbs or copy the code in the below section “JavaUninstallScript.vbs Code” into notepad and save the file as JavaUninstallScript.vbs

  • If running on the local computer
    1. Open the command line as Administrator (Right click on ‘Command Prompt’ in the start menu and select ‘Run as administrator’)
    2. Navigate to where the JavaUninstallScript.vbs was saved to
    3. Run the command which best suits you
  • If running from Configuration Manager (ConfigMgr /  SCCM)
    1. Copy the script to your package share
    2. Create the package
    3. Set the program command as

[crayon attributes]
cscript /nologo JavaUninstallScript.vbs /keeponly
[/crayon]
 

Usage Examples:

[divider style=”thin”]
 
Keeps all Java Runtimes. Only useful for making a list of installed runtimes
[crayon attributes]
cscript /nologo JavaUninstallScript.vbs /removeonly
[/crayon]
 
 
Removes all Java Runtimes without creating logs
[crayon attributes]
cscript /nologo JavaUninstallScript.vbs /keeponly
[/crayon]
 
 
Removes all Java Runtimes found except Java 6 Update 24 and J2SE 5 Update 16 and places the uninstall logs in C:\Temp
[crayon attributes]
cscript /nologo JavaUninstallScript.vbs /keeponly /versions:”Java(TM) 6 Update 24;J2SE Runtime Environment 5.0 Update 16″ /logfilepath:”C:\Temp”
[/crayon]
 
 
Removes all Java Runtimes found except x64 Java 6 Update 24 and x86 J2SE 5 Update 16 on a x64 system and places the uninstall logs in C:\Temp
[crayon attributes]
cscript /nologo JavaUninstallScript.vbs /keeponly /versions:”Java(TM) 6 Update 24″ /versionsx86onx64:”J2SE Runtime Environment 5.0 Update 16″ /logfilepath:”C:\Temp”
[/crayon]
 

JavaUninstallScript.vbs Code

[divider style=”thin”]
 
[crayon attributes]
‘***************************************************************************************
‘Java removal script.
‘Version: 3.0
‘Description: Removes specified Java versions based on command line parameters.
‘                        !!!!!!USE AT YOUR OWN RISK!!!!!!!
‘2011.03.09 – 2.0 – First version. Assumes %SYSTEMDRIVE%\Logs exists.
‘2011.03.23 – 2.1 – Fixes issue running in 32bit SCCM under 64bit Windows.
‘2011.03.30 – 2.2 – Adds array of versions to keep.
‘2011.04.24 – 3.0 – Added command line parameters. Still doesn’t make the log directory!
‘***************************************************************************************
‘—————————–Begin Main Script——————————–
Option Explicit

‘If help is specified on the command line, show it and quit.
If WScript.Arguments.Named.Count < 1 Or WScript.Arguments.Named.Exists(“help”) Or WScript.Arguments.Named.Exists(“?”) Then
PrintHelp()
WScript.Quit(0)
End If

Dim aryVersions, aryVersionsx86Onx64
Dim bolKeepJava
Dim colNamedArguments, colProcesses
Dim objWMIService, objProcessor, objProcess
Dim strArgument, strLogFilePath

‘Set default command line parameters.
‘The script defaults to removing all versions of Java without leaving uninstall logs.
bolKeepJava = 1
strLogFilePath = “”
aryVersions = Array()
aryVersionsx86Onx64 = Array()

‘Start script logging.
WScript.Echo “**********************************”
WScript.Echo “Java uninstall script started at ” & Now()

‘Parse command line parameters.
If WScript.Arguments.Named.Count > 0 Then
Set colNamedArguments = WScript.Arguments.Named

For Each strArgument in colNamedArguments
Select Case LCase(strArgument)
Case “keeponly”
Case “removeonly”
bolKeepJava = 0
Case “logfilepath”
strLogFilePath = colNamedArguments.Item(strArgument)
Case “versions”
aryVersions = Split(colNamedArguments.Item(strArgument), “;”, -1, 1)
Case “versionsx86onx64”
aryVersionsx86Onx64 = Split(colNamedArguments.Item(strArgument), “;”, -1, 1)
Case Else
WScript.Echo vbCrLf & “Unknown switch: ” & strArgument & “.”
WScript.Echo vbCrLf & “Java uninstall script finished at ” & Now()
WScript.Echo “**********************************” & vbCrLf
PrintHelp()
WScript.Quit(2)
End Select
Next
End If

‘Output the parameters the script was run with.
WScript.Echo vbCrLf & “———————————-”
WScript.Echo “Script parameters:”
If bolKeepJava Then
WScript.Echo “Specified Java versions found will be kept. Other versions will be removed.”
Else
WScript.Echo “Specified Java versions found will be removed. Other versions will be kept.”
End If

‘This check is important. It will default the versions array if no versions were specified. Without this the whole thing falls apart.
If (Ubound(aryVersions) < 0 ) Then
aryVersions = Array(“FooBar”)
WScript.Echo “No native Java versions specified on the command line.”
Else
WScript.Echo “Native Java versions specified on the command line.”
End If

‘If no non-native versions are specified set the non-native array to the native array.
If (Ubound(aryVersionsx86Onx64) < 0) Then
aryVersionsx86Onx64 = aryVersions
WScript.Echo “No x86 Java versions for x64 systems specified on the command line.” & vbCrLf & “Using specified native verions for x86 also.”
Else
WScript.Echo “x86 Java versions for x64 systems specified on the command line.”
End If

If strLogFilePath = “” Or IsNull(strLogFilePath) Then
WScript.Echo “Uninstall logfile path is empty. Uninstall logs will not be created.”
Else
‘If the log file path does not end in a \ put one on there!
If (StrComp(Right(strLogFilePath,1), “\”) <> 0) Then
strLogFilePath = strLogFilePath & “\”
End If
WScript.Echo “Uninstall log file path: ” & strLogFilePath & “.”
End If
WScript.Echo “———————————-”

‘Get a WMI Service object.
Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate, (Debug)}\\.\root\cimv2”)

‘Get processor object. objProcessor.AddressWidth will give us bitness. Check objProcessor.AddressWidth = “32” or “64”
Set objProcessor = GetObject(“winmgmts:\\.\root\cimv2:Win32_Processor=’cpu0′”)

‘Kill processes that might prevent installs or uninstalls.
Set colProcesses = objWMIService.ExecQuery(“Select Name from Win32_Process Where Name = ‘jqs.exe’ OR Name = ‘jusched.exe’ OR Name = ‘jucheck.exe’ OR Name = ‘jp2launcher.exe’ OR Name = ‘java.exe’ OR Name = ‘javaws.exe’ OR Name = ‘javaw.exe'”, “WQL”, 48)

WScript.Echo vbCrLf & “———————————-”
WScript.Echo “Checking for problematic processes.”

‘Set this to look for errors that aren’t fatal when killing processes.
On Error Resume Next

‘Cycle through found problematic processes and kill them.
For Each objProcess in colProcesses

WScript.Echo “Found process ” & objProcess.Name & “.”

objProcess.Terminate()

Select Case Err.Number
Case 0
WScript.Echo “Killed process ” & objProcess.Name & “.”
Err.Clear
Case -2147217406
WScript.Echo “Process ” & objProcess.Name & ” already closed.”
Err.Clear
Case Else
WScript.Echo “Could not kill process ” & objProcess.Name & “! Aborting Script!”
WScript.Echo “Error Number: ” & Err.Number
WScript.Echo “Error Description: ” & Err.Description
WScript.Echo “Finished problematic process check.”
WScript.Echo “———————————-”
WScript.Echo vbCrLf & “Java uninstall script finished at ” & Now()
WScript.Echo “**********************************” & vbCrLf
WScript.Quit(1)
End Select

Next

‘Resume normal error handling.
On Error Goto 0

WScript.Echo “Finished problematic process check.”
WScript.Echo “———————————-”

‘This call will remove x64 versions on a x64 system and x86 versions on a x86 system.
RemoveJava “Software\Microsoft\Windows\CurrentVersion\Uninstall\”, aryVersions, strLogFilePath, objProcessor

‘This call will remove x86 versions on a x64 system.
If (objProcessor.AddressWidth = “64”) Then
RemoveJava “Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\”, aryVersionsx86Onx64, strLogFilePath, objProcessor
End If

WScript.Echo vbCrLf & “Java uninstall script finished at ” & Now()
WScript.Echo “**********************************” & vbCrLf
‘——————————-End Main Script——————————–

‘———————————Functions————————————
Function RemoveJava(strRegistryPath, aryVersions, strLogFilePath, objProcessor)

Dim objWSHShell, objRegistry, objWbemContext, objSWbemLocator, objSWbemServices
Dim aryUninstallKeys
Dim strUninstallKey, strDisplayName, strUninstallString, strVersion
Dim intUninstallReturnCode

Set objWSHShell = CreateObject(“WScript.Shell”)

‘The following SWbem setup allows a script running in a x86 context, such as under the SCCM client, on a x64 system
‘to access the full x64 registry. Without this the actual registry paths will be transparently redirected to the
‘Wow6432Node branch for every registry call to HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall.
‘Essentially this transparent redirection would hide 64bit Java runtimes from a script running in 32bit mode on a 64bit OS.

‘Provides the bitness context parameters for registry calls.
Set objWbemContext = CreateObject(“WbemScripting.SWbemNamedValueSet”)
objWbemContext.Add “__ProviderArchitecture”, objProcessor.AddressWidth
objWbemContext.Add “__RequiredArchitecture”, true

‘Create SWbemLocator to connect to WMI
Set objSWbemLocator = CreateObject(“WbemScripting.SWbemLocator”)

‘Actually connect to the WMI service using the SWbemLocator and the SWbemContext parameters.
Set objSWbemServices = objSWbemLocator.ConnectServer(“.”,”root\default”,””,””,,,,objWbemContext)

‘Get the Standard Registry Provider from WMI… finally.
Set objRegistry = objSWbemServices.Get(“StdRegProv”)

‘Find the Java uninstallers hiding in the uninstall key. &H80000002 = HKEY_LOCAL_MACHINE for this function call.
objRegistry.EnumKey &H80000002, strRegistryPath, aryUninstallKeys

‘Enable VBS’ poor excuse for error handling…
On Error Resume Next

For Each strUninstallKey In aryUninstallKeys

‘These must be reset in case the GetStringValue fails to return a value. This way we don’t keep values for other pieces of software.
strDisplayName = “”
strUninstallString = “”
intUninstallReturnCode = “”

‘DisplayName should always be a REG_SZ
objRegistry.GetStringValue &H80000002, strRegistryPath & strUninstallKey, “DisplayName”, strDisplayName

‘Just in case GetStringValue doesn’t retrieve what we want.
If Err.Number <> 0 Then
Wscript.Echo vbCrLf & “———————————-”
WScript.Echo “Could not retrieve DisplayName at ” & strRegistryPath & strUninstallKey & “!”
WScript.Echo “Error Number: ” & Err.Number
WScript.Echo “Error Description: ” & Err.Description
Wscript.Echo “———————————-”
strDisplayName = “”
Err.Clear
End If

‘In English: If the DisplayName contains either Java OR the DisplayName contains J2SE Runtime Environment
‘AND if the DisplayName does not contain Development AND if the DisplayName does not contain JavaDB
‘AND if the DisplayName does not contain Web Start
‘AND if the DisplayName does not contain SAS
‘AND if the DisplayName does not contain Java Auto Update then do the if block.
‘Fun, eh?
‘You could remove the Web Start line to get rid of JWS but the uninstall string If block would have to account for that. It currently doesn’t.

If ((Instr(1, strDisplayName, “Java”, 1) OR (Instr(1, strDisplayName, “J2SE Runtime Environment”, 1))) _
AND ((Instr(1, strDisplayName, “Development”, 1) + Instr(1, strDisplayName, “JavaDB”, 1)) < 1)  _
AND (Instr(1, strDisplayName, “Web Start”, 1) < 1) _
AND (Instr(1, strDisplayName, “SAS”, 1) < 1) _
AND (Instr(1, strDisplayName, “Java Auto Update”, 1) < 1)) Then

Wscript.Echo vbCrLf & “———————————-”
WScript.Echo “Found version: ” & strDisplayName
WScript.Echo “Found at: HKEY_LOCAL_MACHINE\” & strRegistryPath & strUninstallKey

‘UninstallString might be a REG_EXPAND_SZ but GetStringValue should retrieve what we want.
objRegistry.GetStringValue &H80000002, strRegistryPath & strUninstallKey, “UninstallString”, strUninstallString

‘Just in case GetStringValue doesn’t retrieve what we want.
If Err.Number <> 0 Then
Wscript.Echo vbCrLf & “———————————-”
WScript.Echo “Could not retrieve uninstall information for ” & strDisplayName & “!”
WScript.Echo “Error Number: ” & Err.Number
WScript.Echo “Error Description: ” & Err.Description
Wscript.Echo “———————————-”
strUninstallString = “”
Err.Clear
End If

‘Slightly convoluted logic that determines if we’re keeping or removing specific versions.
For Each strVersion In aryVersions
If (bolKeepJava) Then
If (Instr(1, strDisplayName, strVersion, 1) > 0) Then
strUninstallString = “”
End If
Else
If (Instr(1, strDisplayName, strVersion, 1) < 1) Then
strUninstallString = “”
End If
End If
Next

If (strUninstallString <> “”) Then
‘Look for very old JRE 1.1, 1.2.x, or 1.3.0 to 1.3.0_04 and 1.3.1 to 1.3.1_04 InstallShield installs.
If (Instr(1, strUninstallKey, “JRE 1”, 1)) Then
strUninstallString = Replace(strUninstallString, “-f”, “-a -x -y -f”)
‘Look for 1.3.0_05 and 1.3.1_05 to 1.3.1_20 InstallShield based installs.
ElseIf (Instr(1, strUninstallString, “-uninst”, 1)) Then
strUninstallString = “”
WScript.Echo “Java versions 1.3.0_05 and 1.3.1_05 through 1.3.1_20 cannot be silently uninstalled.”
‘Look for a 1.4.0 to 1.4.1_07 InstallShield installation.
ElseIf (Instr(1, strUninstallString, “Anytext”, 1)) Then
‘Create ISS script for this install and fix the uninstall string.
If (CreateISSFile(strUninstallKey, objWSHShell.ExpandEnvironmentStrings(“%TEMP%”))) Then
strUninstallString = Replace(strUninstallString, “Anytext”, “/s /SMS /w /f1″”” & objWSHShell.ExpandEnvironmentStrings(“%TEMP%”) _
& “\” & strUninstallKey & “.iss”””)
‘Check and add the logfile to the uninstaller string.
If (strLogFilePath <> “”) Then
strUninstallString = strUninstallString & ” /f2″”” & strLogFilePath & strDisplayName & “_Uninstall.txt”””
End If
Else
strUninstallString = “”
End If
‘Look for 1.4.2 and up MSI based InstallShield installs.
ElseIf (Instr(1, strUninstallString, “msiexec.exe”, 1)) Then
‘Create MSIEXEC uninstall string.
strUninstallString = “MSIEXEC.EXE /X ” & strUninstallKey & ” /qn /norestart”
‘Check and add the logfile to the uninstaller string.
If (strLogFilePath <> “”) Then
strUninstallString = strUninstallString & ” /l*v “”” & strLogFilePath & strDisplayName & “_Uninstall.txt”””
End If
Else
strUninstallString = “”
End If
Else
strUninstallString = “”
End If

WScript.Echo “Uninstall string: ” & strUninstallString

If (strUninstallString = “”) Then
WScript.Echo strDisplayName & ” was not uninstalled.”
Else
‘Run the uninstaller.
intUninstallReturnCode = objWSHShell.Run(strUninstallString, 0, true)
WScript.Echo “Uninstall return code was: ” & intUninstallReturnCode & “.”
End If

WScript.Echo “———————————-”

End If

Next

‘Resume normal quit on error behavior.
On Error GoTo 0

End Function

Function CreateISSFile(strUninstallKey, strTempPath)
On Error Resume Next

Dim objFileSystem, objUninstallScript

Set objFileSystem  = CreateObject(“Scripting.FileSystemObject”)

‘Create InstallShield ISS script file for the uninstallation.
Set objUninstallScript = objFileSystem.OpenTextFile(strTempPath & “\” & strUninstallKey & “.iss”, 2, True)

If (Err.Number <> 0) Then
WScript.Echo “Could not create uninstall file at ” & strTempPath & “\” & strUninstallKey & “.iss!”
WScript.Echo “Error Number: ” & Err.Number
WScript.Echo “Error Description: ” & Err.Description
Err.Clear
CreateISSFile = 0
End If

‘One ugly write statement to cut down on the ammount of error checking that has to be done.
‘That SharedFile=YesToAll creates problems with multiple versions of 1.4.0 to 1.4.1_07 installed.
objUninstallScript.Write “[InstallShield Silent]” & vbCrLf & “Version=v6.00.000” & vbCrLf & “File=Response File” & vbCrLf & “[File Transfer]” & _
vbCrLf & “OverwrittenReadOnly=NoToAll” & vbCrLf & “[” & strUninstallKey & “-DlgOrder]” & vbCrLf & “Dlg0=” & strUninstallKey & “-SprintfBox-0” & _
vbCrLf & “Count=2” & vbCrLf & “Dlg1=” & strUninstallKey & “-File Transfer” & vbCrLf & “[” & strUninstallKey & “-SprintfBox-0]” & vbCrLf & “Result=1” & _
vbCrLf & “[Application]” & vbCrLf & “Name=Java 2 Runtime Environment, SE v1.4.0” & vbCrLf & “Version=1.4.0” & vbCrLf & “Company=JavaSoft” & _
vbCrLf & “Lang=0009” &     vbCrLf & “[” & strUninstallKey & “-File Transfer]” & vbCrLf & “SharedFile=NoToAll”

If (Err.Number <> 0) Then
WScript.Echo “Could not create uninstall file at ” & strTempPath & “\” & strUninstallKey & “.iss!”
WScript.Echo “Error Number: ” & Err.Number
WScript.Echo “Error Description: ” & Err.Description
Err.Clear
CreateISSFile = 0
End If

objUninstallScript.Close

If (Err.Number <> 0) Then
WScript.Echo “Could not create uninstall file at ” & strTempPath & “\” & strUninstallKey & “.iss!”
WScript.Echo “Error Number: ” & Err.Number
WScript.Echo “Error Description: ” & Err.Description
Err.Clear
CreateISSFile = 0
End If

CreateISSFile = 1

End Function

Function PrintHelp()
‘Just prints out the help when run with /help /? or no arguments.
WScript.Echo vbCrLf & “Java Runtime Environment Removal Script v3.0” & vbCrLf
WScript.Echo “Removes Java runtimes based on command line parameters.” & vbCrLf & “Default parameters removes all Java versions without creating logs.”
WScript.Echo “Does not uninstall Java versions 1.3.0_05 and 1.3.1_05 through 1.3.1_20.” & vbCrLf & “They do not uninstall silently.” & vbCrLf
WScript.Echo “Command line switches:” & vbCrLf
WScript.Echo “/keeponly” & vbTab & vbTab & “Script keeps only versions specified.” & vbCrLf & vbTab & vbTab & vbTab & “If no versions are specified no versions are kept.” & vbCrLf
WScript.Echo “/removeonly” & vbTab & vbTab & “Script removes only versions specified.” & vbCrLf & vbTab & vbTab & vbTab & “If no versions are specified no versions are removed.” & vbCrLf
WScript.Echo “/versions:” & vbTab & vbTab & “Specifies verions to act on.” & vbCrLf & vbTab & vbTab & vbTab & “Versions are seperated by semicolons.” & vbCrLf & vbTab & vbTab & vbTab & _
“By default specified versions are kept.” & vbCrLf & vbTab & vbTab & vbTab & “MUST MATCH THE DISPLAY NAME IN ADD/REMOVE PROGRAMS” & vbCrLf
WScript.Echo “/versionsx86onx64:” & vbTab & “Specifies x86 runtime versions to keep on a x64 system.” & vbCrLf & vbTab & vbTab & vbTab & _
“Versions are seperated by semicolon.” & vbCrLf & vbTab & vbTab & vbTab & _
“If no x86 versions are specified script uses versions”  & vbCrLf & vbTab & vbTab & vbTab & “specified by /versions for both x64 and x86 runtimes.” _
& vbCrLf & vbTab & vbTab & vbTab & “MUST MATCH THE DISPLAY NAME IN ADD/REMOVE PROGRAMS” & vbCrLf
Wscript.Echo “/logfilepath:” & vbTab & vbTab & “Sets path for uninstall log file from Java runtimes.” & vbCrLf & vbTab & vbTab & vbTab & _
“If path does not exist uninstallers will fail.” & vbCrLf
WScript.Echo “Examples:” & vbCrLf
WScript.Echo “cscript /nologo JavaUninstallScript.vbs /keeponly /versions:””Java(TM) 6 Update 24;J2SE Runtime Environment 5.0 Update 16″” /logfilepath:””C:\Temp”””
WScript.Echo “Removes all Java Runtimes found except Java 6 Update 24 and J2SE 5 Update 16 and places the uninstall logs in C:\Temp.” & vbCrLf
WScript.Echo “cscript /nologo JavaUninstallScript.vbs /keeponly /versions:””Java(TM) 6 Update 24″” /versionsx86onx64:””J2SE Runtime Environment 5.0 Update 16″” /logfilepath:””C:\Temp”””
WScript.Echo “Removes all Java Runtimes found except x64 Java 6 Update 24 and x86 J2SE 5 Update 16 on a x64 system and places the uninstall logs in C:\Temp.” & vbCrLf
WScript.Echo “cscript /nologo JavaUninstallScript.vbs /keeponly”
WScript.Echo “Removes all Java Runtimes without creating logs.” & vbCrLf
WScript.Echo “cscript /nologo JavaUninstallScript.vbs /removeonly”
WScript.Echo “Keeps all Java Runtimes. Only useful for making a list of installed runtimes.” & vbCrLf
End Function
[/crayon]