Batch Script to Automatically Uninstall All Legacy Sun Java Versions on Windows XP


10 views

Managing Java versions in an enterprise environment can be challenging, especially when dealing with outdated installations. The main issues with old Sun Java versions include:

  • Security vulnerabilities in older JREs
  • Multiple versions coexisting causing conflicts
  • Registry clutter from incomplete uninstalls
  • Potential for browsers to load outdated plugins

The original batch script attempts to:

  1. Query the Windows registry for installed programs
  2. Filter for Sun-published applications
  3. Further filter for Java-related installations
  4. Execute MSI uninstall commands

The error occurs because the script doesn't properly handle registry keys containing spaces or special characters. Here's an improved version:

@echo off
setlocal enabledelayedexpansion

echo Searching for legacy Sun Java installations...

for /f "tokens=*" %%A in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s /f "Sun" ^| find "HKEY_"') do (
    for /f "tokens=3" %%B in ('reg query "%%A" /v Publisher 2^>nul ^| find "Sun"') do (
        for /f "tokens=3" %%C in ('reg query "%%A" /v DisplayName 2^>nul ^| find "Java"') do (
            echo Found: %%C
            for /f "tokens=2*" %%D in ('reg query "%%A" /v UninstallString 2^>nul') do (
                set "uninstall=%%D %%E"
                if "!uninstall:~0,9!"=="MsiExec." (
                    echo Uninstalling: %%C
                    start /wait !uninstall! /quiet /norestart
                ) else (
                    echo Found non-MSI install: !uninstall!
                )
            )
        )
    )
)

echo Search complete.
endlocal

This improved script includes better error handling and logging:

@echo off
setlocal enabledelayedexpansion

set LOGFILE=%TEMP%\JavaUninstall.log
echo Java Uninstallation Log - %DATE% %TIME% > %LOGFILE%
echo. >> %LOGFILE%

echo Scanning for legacy Sun Java installations...
echo Scanning for legacy Sun Java installations... >> %LOGFILE%

for /f "tokens=*" %%A in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s /f "Sun" ^| find "HKEY_"') do (
    echo Processing registry key: %%A >> %LOGFILE%
    
    for /f "tokens=3" %%B in ('reg query "%%A" /v Publisher 2^>nul ^| find "Sun"') do (
        for /f "tokens=3" %%C in ('reg query "%%A" /v DisplayName 2^>nul ^| find "Java"') do (
            echo Found Sun Java installation: %%C >> %LOGFILE%
            echo Found: %%C
            
            for /f "tokens=2*" %%D in ('reg query "%%A" /v UninstallString 2^>nul') do (
                set "uninstall=%%D %%E"
                set "uninstall=!uninstall:"=!"
                
                if "!uninstall:~0,9!"=="MsiExec." (
                    echo Attempting uninstall of: %%C >> %LOGFILE%
                    echo !uninstall! /quiet /norestart >> %LOGFILE%
                    
                    echo Uninstalling: %%C
                    start /wait !uninstall! /quiet /norestart
                    
                    if !ERRORLEVEL! equ 0 (
                        echo Successfully uninstalled: %%C >> %LOGFILE%
                    ) else (
                        echo Failed to uninstall: %%C (Error !ERRORLEVEL!) >> %LOGFILE%
                    )
                ) else (
                    echo Non-MSI installation detected: !uninstall! >> %LOGFILE%
                )
            )
        )
    )
)

echo. >> %LOGFILE%
echo Uninstallation process completed. >> %LOGFILE%
echo Scan complete. See %LOGFILE% for details.
endlocal

For enterprise deployment through System Center:

  • Package the batch script as a standalone executable using PS2EXE
  • Create a silent MSI wrapper using tools like Advanced Installer
  • Consider using PowerShell for more advanced functionality

After running the uninstaller, verify removal by:

@echo off
echo Checking remaining Java installations...
wmic product where "vendor like '%%Sun%%'" get name
wmic product where "name like '%%Java%%'" get name,version
echo Verification complete.
pause

For complete cleanup:

  • Manually remove leftover Java folders in Program Files
  • Clean browser plugins (npjp2.dll) from Firefox/Chrome
  • Update environment variables if needed
  • Consider using JavaRa for stubborn installations

Managing Java installations across an enterprise environment presents unique challenges. The silent installers for newer Java versions don't automatically remove older installations, leaving systems vulnerable to security exploits targeting legacy versions (some as old as Java 1.4 in our case). With Oracle's acquisition of Sun Microsystems, we can leverage the publisher name change as a clean way to identify legacy installations.

The batch script you found attempts to:

1. Query the Windows uninstall registry key
2. Filter for Sun-published installations
3. Identify Java-specific products
4. Execute silent uninstalls via Msiexec

The errors you're seeing occur because the script doesn't properly handle registry keys containing spaces or special characters. The for /f command interprets these as separate commands.

Here's a more robust version that handles special cases:

@echo off
setlocal enabledelayedexpansion

echo Searching for legacy Sun Java installations...

:: Main registry query with proper parsing
for /f "tokens=*" %%I in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f "Sun" /k') do (
    set "regkey=%%I"
    if "!regkey:~0,5!"=="HKEY_" (
        for /f "tokens=2*" %%P in ('reg query "!regkey!" /v Publisher 2^>nul') do (
            echo %%Q | find /i "Sun" >nul && (
                for /f "tokens=2*" %%N in ('reg query "!regkey!" /v DisplayName 2^>nul') do (
                    echo %%O | find /i "Java" >nul && (
                        echo Found: %%O
                        for /f "tokens=2*" %%G in ('reg query "!regkey!" /v UninstallString 2^>nul') do (
                            set "uninstall=%%G %%H"
                            set "uninstall=!uninstall:/I= /x!"
                            echo Uninstalling: %%O
                            start /wait "" !uninstall! /quiet /noreboot
                        )
                    )
                )
            )
        )
    )
)

echo Legacy Java removal complete.
endlocal
  • Proper handling of registry keys with spaces
  • More accurate publisher filtering (case-insensitive)
  • Better error handling for missing values
  • Explicit quiet uninstall parameters

For systems where registry queries aren't ideal, consider this WMIC-based solution:

@echo off
for /f "tokens=2 delims==" %%A in (
    'wmic product where "vendor like '%%Sun%%'" get IdentifyingNumber /value ^| find "IdentifyingNumber"'
) do (
    echo Uninstalling product ID: %%A
    wmic product where "IdentifyingNumber='%%A'" call uninstall /nointeractive
)

To deploy via System Center Essentials:

  1. Save the script as RemoveLegacyJava.cmd
  2. Create a basic MSI wrapper using WiX or similar tool
  3. Set execution context to SYSTEM
  4. Add proper exit codes for reporting

After running the script, verify removal with:

wmic product where "name like '%%Java%%'" get name, version