Something I’ve wanted to do for a while was to write a script to download and install the latest Java 7 update from Oracle. I’ve been using AutoPkg to download the latest Java 7 updates using AutoPkg’s OracleJava7 recipes, but I wanted to develop a script that would do the following:
- Download the latest Java 7 installer from Oracle’s website
- Install the latest Java 7 update
- Clean up after itself
Oracle didn’t make this an easy task, as the download URL seems to change on a per-update version. AutoPkg handles its update task by scraping Oracle’s manual download page for the current correct URL to use.
Oracle does provide a Sparkle-based update mechanism for Java 7 on OS X, so I wanted to see if there was a way to leverage that to pull down updates. The only address I could find in that regard was the SUFeedURL value included in Java 7’s /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist file. I checked that value using the following command:
defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info" SUFeedURL
The output I received for Java 7 Update 67 was the following:
https://javadl-esd-secure.oracle.com/update/mac/au-1.7.0_67.xml
I decided to see what output would come back from Oracle’s site when accessed, so I used the following curl command to see what was returned:
/usr/bin/curl --silent https://javadl-esd-secure.oracle.com/update/mac/au-1.7.0_67.xml
The following XML was returned and I was gratified to see that it contained a download link to a Java 7 Update 67 disk image.
Image may be NSFW.
Clik here to view.
One of the important things I was able to establish is that the XML address embedded with Java 7 Update 67 is not special in this regard. As part of my testing, I verified that using the SUFeedURL value for Java 7 Update 15 and 65 will also work to pull the address of the latest Oracle Java 7 installer disk image.
Using this information, I was able to build a script that can download and install the latest Java 7 update. See below the jump for details.
Update 3-6-2015: This script has been retired and should no longer be used to install Java 7. It uses Java 7’s update feed to install the latest Java and Oracle has recently updated the feed to begin providing Java 8 to Java 7 users:
http://www.oracle.com/technetwork/java/javase/documentation/autoupdatejre7tojre8-2389085.html
The script below will download a disk image containing the latest version of Java 7 from Oracle and install Java 7 using the installer package stored inside the downloaded disk image.
How the script works:
- Uses curl to download a disk image containing the latest Java 7 installer from Oracle’s web site.
- Renames the downloaded disk image to java_seven.dmg and stores it in /tmp.
- Mounts the disk image silently in /tmp. The mounted disk image will not be visible to any logged-in user.
- Installs the latest version of Java 7 using the installer package stored on the disk image.
- After installation, unmounts the disk image and removes it from the Mac in question.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script downloads and installs the latest Oracle Java 7 for compatible Macs | |
# Determine OS version | |
osvers=$(sw_vers -productVersion | awk -F. '{print $2}') | |
# Including an OS check to pull the minor version number. | |
# This is to help provide a check for 10.7.0 through 10.7.2, | |
# as Oracle's Java 7 only runs on 10.7.3 and higher. | |
osminorvers=$(sw_vers -productVersion | awk -F. '{print $3}') | |
# Specify the "OracleUpdateXML" variable by adding the "SUFeedURL" value included in the | |
# /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist file. | |
# | |
# Note: The "OracleUpdateXML" variable is currently specified in the script as that for | |
# Java 7 Update 67, but the XML address embedded with Java 7 Update 67 is not special in | |
# this regard. I have verified that using the address for Java 7 Update 15 and 65 will | |
# also work to pull the address of the latest Oracle Java 7 installer disk image. To get | |
# the "SUFeedURL" value embedded with your currently installed version of Java 7 on Mac OS X, | |
# please run the following command in Terminal: | |
# | |
# defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info" SUFeedURL | |
# | |
# As of Java 7 Update 67, that produces the following return: | |
# | |
# https://javadl-esd-secure.oracle.com/update/mac/au-1.7.0_67.xml | |
# | |
OracleUpdateXML="https://javadl-esd-secure.oracle.com/update/mac/au-1.7.0_67.xml" | |
# Use the XML address defined in the OracleUpdateXML variable to query Oracle via curl | |
# for the complete address of the latest Oracle Java 7 installer disk image. | |
fileURL=`/usr/bin/curl –silent $OracleUpdateXML | awk -F \" /enclosure/'{print $(NF-1)}'` | |
# Specify name of downloaded disk image | |
java_seven_dmg="/tmp/java_seven.dmg" | |
if [[ ${osvers} -lt 7 ]]; then | |
echo "Oracle Java 7 is not available for Mac OS X 10.6.8 or below." | |
fi | |
if [[ ${osvers} -ge 7 ]]; then | |
if [[ ${osvers} -eq 7 ]]; then | |
if [[ ${osminorvers} -lt 3 ]]; then | |
echo "Oracle Java 7 is not available for Mac OS X 10.7.2 or below." | |
exit 0 | |
fi | |
fi | |
# Download the latest Oracle Java 7 software disk image | |
# The curl -L option is needed because there is a redirect | |
# that the requested page has moved to a different location. | |
/usr/bin/curl –retry 3 -Lo "$java_seven_dmg" "$fileURL" | |
# Specify a /tmp/java_seven.XXXX mountpoint for the disk image | |
TMPMOUNT=`/usr/bin/mktemp -d /tmp/java_seven.XXXX` | |
# Mount the latest Oracle Java 7 disk image to /tmp/java_seven.XXXX mountpoint | |
hdiutil attach "$java_seven_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen | |
# Install Oracle Java 7 from the installer package stored inside the disk image | |
/usr/sbin/installer -dumplog -verbose -pkg "$(/usr/bin/find $TMPMOUNT -maxdepth 1 \( -iname \*\.pkg -o -iname \*\.mpkg \))" -target "/" | |
# Clean-up | |
# Unmount the Oracle Java 7 disk image from /tmp/java_seven.XXXX | |
/usr/bin/hdiutil detach -force "$TMPMOUNT" | |
# Remove the /tmp/java_seven.XXXX mountpoint | |
/bin/rm -rf "$TMPMOUNT" | |
# Remove the downloaded disk image | |
/bin/rm -rf "$java_seven_dmg" | |
fi | |
exit 0 |
I’ve posted the script to my GitHub repo at the following address:
This script is also available as a payload-free installer package, stored as a .zip file in the payload_free_installer directory.