Skip to content

Instantly share code, notes, and snippets.

@leovandriel
Last active September 12, 2017 17:07
Show Gist options
  • Save leovandriel/8461111abd7b6ece517ccdf5657d123b to your computer and use it in GitHub Desktop.
Save leovandriel/8461111abd7b6ece517ccdf5657d123b to your computer and use it in GitHub Desktop.
#!/bin/bash
## Install Java Development Kit (JDK) without all the junk
# Strips the non-jdk stuff (Updater, Applet, Web Start, file associations) out of the installer.
# NB: this also removes the pre-installation checks and will install the JDK unconditionally
# to /Library/Java/JavaVirtualMachines/ with possible but unlikely dire consequences
## Usage
set -e
if [[ -z "$2" ]] ; then
echo 'usage: ./strip-jdk-installer dmgin dmgout'
exit 0
fi
if [ ! -f "$1" ]; then
echo "$1 does not exist"
exit 0
fi
if [ -f "$2" ]; then
echo "$2 already exists"
exit 0
fi
TEMP_DIR="$(mktemp -d)/jdk"
## Unpack
INSTALLER_VOL=$(hdiutil attach "$1" | grep -oe "/Volumes/.*")
INSTALLER_PKG=$(ls -1 "$INSTALLER_VOL" | grep -oe "^.*pkg$")
pkgutil --expand "$INSTALLER_VOL/$INSTALLER_PKG" "$TEMP_DIR"
hdiutil detach "$INSTALLER_VOL" > /dev/null
JDK_PKG=$(ls -1 "$TEMP_DIR" | grep -oe "^jdk.*pkg$")
## Strip JDK
# disable Java Flight Recorder (JFR)
sed -i '' '/^\(\.\/\)/d' "$TEMP_DIR/$JDK_PKG/Scripts/postinstall"
rm "$TEMP_DIR/$JDK_PKG/Scripts/Tools"
# disable Java Mission Control (JMC)
sed -i '' '/com.oracle.jmc/d' "$TEMP_DIR/$JDK_PKG/PackageInfo"
## Repackage
INSTALLER_NAME=$(echo "$INSTALLER_PKG" | grep -oe "[^.]*")
mkdir -p "$TEMP_DIR/installer"
pkgutil --flatten "$TEMP_DIR/$JDK_PKG" "$TEMP_DIR/installer/$INSTALLER_PKG"
hdiutil create -volname "$INSTALLER_NAME" -srcfolder "$TEMP_DIR/installer" $2
rm -rf "$TEMP_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment