Skip to main content
Solved

JAVA Uninstall Script


Forum|alt.badge.img+9
  • Valued Contributor
  • 114 replies

Forgive me if this is answered in another discussion - I have seen similar posts, but nothing that gets to the root of what I am trying to accomplish. I am also, unfortunately not versed in BASH scripting, hence the reason for reaching out...

I am trying to put together a policy that includes a script to completely remove any installed versions of Oracle JAVA - including multiple JDK versions if found.

I am familiar with the official method published here: https://www.java.com/en/download/help/mac_uninstall_java.xml

However, if a machine has multiple JDK versions installed, I need a way to detect those and loop through an uninstall command to remove each.

Does anyone have a script that will accomplish this?

Thanks for any insight that can be provided...

Brian

Best answer by davidhiggs

We're going down the path of removing all Oracle Java variants from our environment, with a view to offer something like AdoptOpenJDK. Here's what my colleague has worked on so far. Please test thoroughly as we've not completed our testing. Hope it helps!

#!/bin/bash

# Master script to delete Sun/Oracle Java installations from MacOS X
# Billy Constantine, The University of Adelaide, 2019

# There are a couple of places to find Java and Java-related files in MacOS X:
# - "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" is the web browser
#   plugin.
# - "/Library/PreferencePanes/JavaControlPanel.prefPane" is the System
#   Preferences applet.
# - "$HOME/Library/Application Support/Oracle/Java" is some kind of installation
#   cache.
# - "/Library/Java/JavaVirtualMachines" contains all installed instances of both
#   the JDK and JRE, both Sun/Oracle and OpenJDK (and others?).
# - there may be other instances through /Library and $HOME/Library, mostly with
#   names that begin with "com.oracle.java"; one sure spot is
#   "/Library/Application Support/Oracle/Java", and it's probably worth checking
#   "/Library/Application Support/Sun/Java" too.
# - /private/var/db/receipts may also have files that begin with
#   "com.oracle.jdk" and/or "com.oracle.jre".

# So:
# - delete the plugin;
# - delete the System Preferences applet;
# - scan all non-system user accounts and delete installation caches;
# - check all installed JVMs and delete the Sun/Oracle ones.

# We'll split this into two parts:
# - we'll combine the deletions of the plugin, applet, and user caches into a
#   single step, since we're unconditionally deleting all those;
# - then we'll check each JVM and (1) leave the "openjdk" ones, (2) delete the
#   "java" ones, and (3) leave any others but flag them to the user.

echo ' '
if [ `/usr/bin/id -u` != 0 ]; then
   # Warn if run as a non-superuser
   echo WARNING
   echo WARNING: not running as superuser
   echo WARNING: system-wide Java cleanup may not be possible
   echo WARNING: try running "sudo $0" instead
   echo WARNING
   echo ' '
fi

# For debugging/review
# Uncomment the second line when ready
RM='echo /bin/rm'
#RM=/bin/rm

# Part one
echo Removing web browser plugin, System Preferences applet, and user installation caches
TO_BE_CHECKED=`/usr/bin/perl -le '@d=("/");setpwent();while(@F=getpwent()){unless(($F[7]=~m@^/(private/)?var@)||($F[8] eq "/usr/bin/false")){push(@d, "$F[7]/");}}endpwent();END{print(join(":", grep { -d } @d));}'`
SUBS="Library/Internet Plug-Ins/JavaAppletPlugin.plugin:Library/PreferencePanes/JavaControlPanel.prefPane:Library/Application Support/Sun/Java:Library/Application Support/Oracle/Java"
OIFS=${IFS}
IFS=':'
for d in ${TO_BE_CHECKED}; do
   for s in ${SUBS}; do
      f="${d}${s}"
      if [ -d "${f}" ]; then
         echo Deleting "${f}"
         echo ${RM} -rf ""${f}"" | /bin/bash -f
      fi
   done
   l="${d}Library"
   for s in `/usr/bin/find "${l}" -name 'com.oracle.java*' -exec echo -n {}: ; 2>/dev/null`; do
      echo Deleting "${s}"
      echo ${RM} -rf ""${s}"" | /bin/bash -f
   done
done
IFS=${OIFS}
for f in `/usr/bin/find /private/var/db/receipts ( -name 'com.oracle.jre*' -o -name 'com.oracle.jdk*' ) 2>/dev/null`; do
   echo Deleting "${f}"
   echo ${RM} -rf ""${f}"" | /bin/bash -f   
done
echo ' '

# Part two
echo Removing non-OpenJDK JVMs
for _jvm in `/usr/bin/find /Library/Java/JavaVirtualMachines -mindepth 1 -maxdepth 1 -type d`; do
   _java=${_jvm}/Contents/Home/bin/java
   if [ -f "${_java}" ]; then
      _type=`"${_java}" -version 2>&1 | /usr/bin/head -1 | /usr/bin/awk '{print $1}'`
      if [ "${_type}" == "openjdk" ]; then
         echo Leaving ${_jvm}
      elif [ "${_type}" == "java" ]; then
         echo Deleting ${_jvm}
         echo ${RM} -fr "${_jvm}" | /bin/bash -f
      else
         echo Unknown JVM designation "${_type}" for ${_jvm}, leaving
      fi
   fi
done
View original
Did this topic help you find an answer to your question?

6 replies

Forum|alt.badge.img+9
  • Author
  • Valued Contributor
  • 114 replies
  • July 31, 2019

bump :)


Forum|alt.badge.img+11
  • Valued Contributor
  • 183 replies
  • Answer
  • August 1, 2019

We're going down the path of removing all Oracle Java variants from our environment, with a view to offer something like AdoptOpenJDK. Here's what my colleague has worked on so far. Please test thoroughly as we've not completed our testing. Hope it helps!

#!/bin/bash

# Master script to delete Sun/Oracle Java installations from MacOS X
# Billy Constantine, The University of Adelaide, 2019

# There are a couple of places to find Java and Java-related files in MacOS X:
# - "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" is the web browser
#   plugin.
# - "/Library/PreferencePanes/JavaControlPanel.prefPane" is the System
#   Preferences applet.
# - "$HOME/Library/Application Support/Oracle/Java" is some kind of installation
#   cache.
# - "/Library/Java/JavaVirtualMachines" contains all installed instances of both
#   the JDK and JRE, both Sun/Oracle and OpenJDK (and others?).
# - there may be other instances through /Library and $HOME/Library, mostly with
#   names that begin with "com.oracle.java"; one sure spot is
#   "/Library/Application Support/Oracle/Java", and it's probably worth checking
#   "/Library/Application Support/Sun/Java" too.
# - /private/var/db/receipts may also have files that begin with
#   "com.oracle.jdk" and/or "com.oracle.jre".

# So:
# - delete the plugin;
# - delete the System Preferences applet;
# - scan all non-system user accounts and delete installation caches;
# - check all installed JVMs and delete the Sun/Oracle ones.

# We'll split this into two parts:
# - we'll combine the deletions of the plugin, applet, and user caches into a
#   single step, since we're unconditionally deleting all those;
# - then we'll check each JVM and (1) leave the "openjdk" ones, (2) delete the
#   "java" ones, and (3) leave any others but flag them to the user.

echo ' '
if [ `/usr/bin/id -u` != 0 ]; then
   # Warn if run as a non-superuser
   echo WARNING
   echo WARNING: not running as superuser
   echo WARNING: system-wide Java cleanup may not be possible
   echo WARNING: try running "sudo $0" instead
   echo WARNING
   echo ' '
fi

# For debugging/review
# Uncomment the second line when ready
RM='echo /bin/rm'
#RM=/bin/rm

# Part one
echo Removing web browser plugin, System Preferences applet, and user installation caches
TO_BE_CHECKED=`/usr/bin/perl -le '@d=("/");setpwent();while(@F=getpwent()){unless(($F[7]=~m@^/(private/)?var@)||($F[8] eq "/usr/bin/false")){push(@d, "$F[7]/");}}endpwent();END{print(join(":", grep { -d } @d));}'`
SUBS="Library/Internet Plug-Ins/JavaAppletPlugin.plugin:Library/PreferencePanes/JavaControlPanel.prefPane:Library/Application Support/Sun/Java:Library/Application Support/Oracle/Java"
OIFS=${IFS}
IFS=':'
for d in ${TO_BE_CHECKED}; do
   for s in ${SUBS}; do
      f="${d}${s}"
      if [ -d "${f}" ]; then
         echo Deleting "${f}"
         echo ${RM} -rf ""${f}"" | /bin/bash -f
      fi
   done
   l="${d}Library"
   for s in `/usr/bin/find "${l}" -name 'com.oracle.java*' -exec echo -n {}: ; 2>/dev/null`; do
      echo Deleting "${s}"
      echo ${RM} -rf ""${s}"" | /bin/bash -f
   done
done
IFS=${OIFS}
for f in `/usr/bin/find /private/var/db/receipts ( -name 'com.oracle.jre*' -o -name 'com.oracle.jdk*' ) 2>/dev/null`; do
   echo Deleting "${f}"
   echo ${RM} -rf ""${f}"" | /bin/bash -f   
done
echo ' '

# Part two
echo Removing non-OpenJDK JVMs
for _jvm in `/usr/bin/find /Library/Java/JavaVirtualMachines -mindepth 1 -maxdepth 1 -type d`; do
   _java=${_jvm}/Contents/Home/bin/java
   if [ -f "${_java}" ]; then
      _type=`"${_java}" -version 2>&1 | /usr/bin/head -1 | /usr/bin/awk '{print $1}'`
      if [ "${_type}" == "openjdk" ]; then
         echo Leaving ${_jvm}
      elif [ "${_type}" == "java" ]; then
         echo Deleting ${_jvm}
         echo ${RM} -fr "${_jvm}" | /bin/bash -f
      else
         echo Unknown JVM designation "${_type}" for ${_jvm}, leaving
      fi
   fi
done

Forum|alt.badge.img+9
  • Author
  • Valued Contributor
  • 114 replies
  • August 1, 2019

David - thanks a ton. I will give this a go. Really appreciate you sharing!

Brian


Forum|alt.badge.img+9
  • Author
  • Valued Contributor
  • 114 replies
  • August 1, 2019

Update - Script works great! Thanks again for your help!

Brian


Forum|alt.badge.img+5
  • Contributor
  • 32 replies
  • August 28, 2023
davidhiggs wrote:

We're going down the path of removing all Oracle Java variants from our environment, with a view to offer something like AdoptOpenJDK. Here's what my colleague has worked on so far. Please test thoroughly as we've not completed our testing. Hope it helps!

#!/bin/bash

# Master script to delete Sun/Oracle Java installations from MacOS X
# Billy Constantine, The University of Adelaide, 2019

# There are a couple of places to find Java and Java-related files in MacOS X:
# - "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" is the web browser
#   plugin.
# - "/Library/PreferencePanes/JavaControlPanel.prefPane" is the System
#   Preferences applet.
# - "$HOME/Library/Application Support/Oracle/Java" is some kind of installation
#   cache.
# - "/Library/Java/JavaVirtualMachines" contains all installed instances of both
#   the JDK and JRE, both Sun/Oracle and OpenJDK (and others?).
# - there may be other instances through /Library and $HOME/Library, mostly with
#   names that begin with "com.oracle.java"; one sure spot is
#   "/Library/Application Support/Oracle/Java", and it's probably worth checking
#   "/Library/Application Support/Sun/Java" too.
# - /private/var/db/receipts may also have files that begin with
#   "com.oracle.jdk" and/or "com.oracle.jre".

# So:
# - delete the plugin;
# - delete the System Preferences applet;
# - scan all non-system user accounts and delete installation caches;
# - check all installed JVMs and delete the Sun/Oracle ones.

# We'll split this into two parts:
# - we'll combine the deletions of the plugin, applet, and user caches into a
#   single step, since we're unconditionally deleting all those;
# - then we'll check each JVM and (1) leave the "openjdk" ones, (2) delete the
#   "java" ones, and (3) leave any others but flag them to the user.

echo ' '
if [ `/usr/bin/id -u` != 0 ]; then
   # Warn if run as a non-superuser
   echo WARNING
   echo WARNING: not running as superuser
   echo WARNING: system-wide Java cleanup may not be possible
   echo WARNING: try running "sudo $0" instead
   echo WARNING
   echo ' '
fi

# For debugging/review
# Uncomment the second line when ready
RM='echo /bin/rm'
#RM=/bin/rm

# Part one
echo Removing web browser plugin, System Preferences applet, and user installation caches
TO_BE_CHECKED=`/usr/bin/perl -le '@d=("/");setpwent();while(@F=getpwent()){unless(($F[7]=~m@^/(private/)?var@)||($F[8] eq "/usr/bin/false")){push(@d, "$F[7]/");}}endpwent();END{print(join(":", grep { -d } @d));}'`
SUBS="Library/Internet Plug-Ins/JavaAppletPlugin.plugin:Library/PreferencePanes/JavaControlPanel.prefPane:Library/Application Support/Sun/Java:Library/Application Support/Oracle/Java"
OIFS=${IFS}
IFS=':'
for d in ${TO_BE_CHECKED}; do
   for s in ${SUBS}; do
      f="${d}${s}"
      if [ -d "${f}" ]; then
         echo Deleting "${f}"
         echo ${RM} -rf ""${f}"" | /bin/bash -f
      fi
   done
   l="${d}Library"
   for s in `/usr/bin/find "${l}" -name 'com.oracle.java*' -exec echo -n {}: ; 2>/dev/null`; do
      echo Deleting "${s}"
      echo ${RM} -rf ""${s}"" | /bin/bash -f
   done
done
IFS=${OIFS}
for f in `/usr/bin/find /private/var/db/receipts ( -name 'com.oracle.jre*' -o -name 'com.oracle.jdk*' ) 2>/dev/null`; do
   echo Deleting "${f}"
   echo ${RM} -rf ""${f}"" | /bin/bash -f   
done
echo ' '

# Part two
echo Removing non-OpenJDK JVMs
for _jvm in `/usr/bin/find /Library/Java/JavaVirtualMachines -mindepth 1 -maxdepth 1 -type d`; do
   _java=${_jvm}/Contents/Home/bin/java
   if [ -f "${_java}" ]; then
      _type=`"${_java}" -version 2>&1 | /usr/bin/head -1 | /usr/bin/awk '{print $1}'`
      if [ "${_type}" == "openjdk" ]; then
         echo Leaving ${_jvm}
      elif [ "${_type}" == "java" ]; then
         echo Deleting ${_jvm}
         echo ${RM} -fr "${_jvm}" | /bin/bash -f
      else
         echo Unknown JVM designation "${_type}" for ${_jvm}, leaving
      fi
   fi
done

@davidhiggs Do you happen to have an updated version on this script?  I tried it as is on Ventura and got the following: 

sudo sh jamf-remove-Java.sh Password: Removing web browser plugin, System Preferences applet, and user installation caches find: -exec: no terminating ";" or "+" find: -exec: no terminating ";" or "+" find: -exec: no terminating ";" or "+" jamf-remove-Java.sh: command substitution: line 74: syntax error near unexpected token `(' jamf-remove-Java.sh: command substitution: line 74: `/usr/bin/find /private/var/db/receipts ( -name 'com.oracle.jre*' -o -name 'com.oracle.jdk*' ) 2>/dev/null' Removing non-OpenJDK JVMs

I thought I would ask before spending a lot of time trying to debug.  


Forum|alt.badge.img+6
  • New Contributor
  • 9 replies
  • October 16, 2024
chelm wrote:

@davidhiggs Do you happen to have an updated version on this script?  I tried it as is on Ventura and got the following: 

sudo sh jamf-remove-Java.sh Password: Removing web browser plugin, System Preferences applet, and user installation caches find: -exec: no terminating ";" or "+" find: -exec: no terminating ";" or "+" find: -exec: no terminating ";" or "+" jamf-remove-Java.sh: command substitution: line 74: syntax error near unexpected token `(' jamf-remove-Java.sh: command substitution: line 74: `/usr/bin/find /private/var/db/receipts ( -name 'com.oracle.jre*' -o -name 'com.oracle.jdk*' ) 2>/dev/null' Removing non-OpenJDK JVMs

I thought I would ask before spending a lot of time trying to debug.  


@chelm I corrected the syntax errors and tested, this worked for me

#!/bin/bash # Master script to delete Sun/Oracle Java installations from MacOS X # Billy Constantine, The University of Adelaide, 2019 # There are a couple of places to find Java and Java-related files in MacOS X: # - "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" is the web browser # plugin. # - "/Library/PreferencePanes/JavaControlPanel.prefPane" is the System # Preferences applet. # - "$HOME/Library/Application Support/Oracle/Java" is some kind of installation # cache. # - "/Library/Java/JavaVirtualMachines" contains all installed instances of both # the JDK and JRE, both Sun/Oracle and OpenJDK (and others?). # - there may be other instances through /Library and $HOME/Library, mostly with # names that begin with "com.oracle.java"; one sure spot is # "/Library/Application Support/Oracle/Java", and it's probably worth checking # "/Library/Application Support/Sun/Java" too. # - /private/var/db/receipts may also have files that begin with # "com.oracle.jdk" and/or "com.oracle.jre". # So: # - delete the plugin; # - delete the System Preferences applet; # - scan all non-system user accounts and delete installation caches; # - check all installed JVMs and delete the Sun/Oracle ones. # We'll split this into two parts: # - we'll combine the deletions of the plugin, applet, and user caches into a # single step, since we're unconditionally deleting all those; # - then we'll check each JVM and (1) leave the "openjdk" ones, (2) delete the # "java" ones, and (3) leave any others but flag them to the user. echo ' ' if [ `/usr/bin/id -u` != 0 ]; then # Warn if run as a non-superuser echo WARNING echo WARNING: not running as superuser echo WARNING: system-wide Java cleanup may not be possible echo WARNING: try running "sudo $0" instead echo WARNING echo ' ' fi # For debugging/review # Uncomment the second line when ready RM='echo /bin/rm' #RM=/bin/rm # Part one echo Removing web browser plugin, System Preferences applet, and user installation caches TO_BE_CHECKED=`/usr/bin/perl -le '@d=("/");setpwent();while(@F=getpwent()){unless(($F[7]=~m@^/(private/)?var@)||($F[8] eq "/usr/bin/false")){push(@d, "$F[7]/");}}endpwent();END{print(join(":", grep { -d } @d));}'` SUBS="Library/Internet Plug-Ins/JavaAppletPlugin.plugin:Library/PreferencePanes/JavaControlPanel.prefPane:Library/Application Support/Sun/Java:Library/Application Support/Oracle/Java" OIFS=${IFS} IFS=':' for d in ${TO_BE_CHECKED}; do for s in ${SUBS}; do f="${d}${s}" if [ -d "${f}" ]; then echo Deleting "${f}" echo ${RM} -rf ""${f}"" | /bin/bash -f fi done l="${d}Library" for s in `/usr/bin/find "${l}" -name 'com.oracle.java*' -exec echo -n {}: 2>/dev/null`; do echo Deleting "${s}" echo ${RM} -rf ""${s}"" | /bin/bash -f done done IFS=${OIFS} for f in `/usr/bin/find /private/var/db/receipts -name 'com.oracle.jre*' -o -name 'com.oracle.jdk*' 2>/dev/null`; do echo Deleting "${f}" echo ${RM} -rf ""${f}"" | /bin/bash -f done echo ' ' # Part two echo Removing non-OpenJDK JVMs for _jvm in `/usr/bin/find /Library/Java/JavaVirtualMachines -mindepth 1 -maxdepth 1 -type d`; do _java=${_jvm}/Contents/Home/bin/java if [ -f "${_java}" ]; then _type=`"${_java}" -version 2>&1 | /usr/bin/head -1 | /usr/bin/awk '{print $1}'` if [ "${_type}" == "openjdk" ]; then echo Leaving ${_jvm} elif [ "${_type}" == "java" ]; then echo Deleting ${_jvm} echo ${RM} -fr "${_jvm}" | /bin/bash -f else echo Unknown JVM designation "${_type}" for ${_jvm}, leaving fi fi done

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings