Autoupdate Java by shell

peterlbk
Contributor

Hi all!

inspired by the great @rtrouton , I adjusted his superb script on installing Java.

I added a check to see if java has been installed or if a newer version is available, a bit like my Flash Update script, for which I borrowed heavily on other guys work.

Hope you guys can use this, and if you have any suggestions, please let me know!

#!/bin/bash -e

# This script downloads and installs the latest Oracle Java 8 for compatible Macs

# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# 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 8 Update 20, but the XML address embedded with Java 8 Update 20 is not special in 
# this regard. I have verified that using the address for Java 8 Update 5 will also work 
# to pull the address of the latest Oracle Java 8 installer disk image. To get the "SUFeedURL"
# value embedded with your currently installed version of Java 8 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 8 Update 20, that produces the following return:
#
# https://javadl-esd-secure.oracle.com/update/mac/au-1.8.0_20.xml
#

OracleUpdateXML="https://javadl-esd-secure.oracle.com/update/mac/au-1.8.0_20.xml"

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=${currentver:5:1}
    echo "Installed main version is $currentvermain"
    currentvermin=${currentver:14:2}
    echo "Installed minor version is $currentvermin"
    /usr/bin/curl --retry 3 -Lo /tmp/au-1.8.0_20.xml $OracleUpdateXML
    onlineversion1=`cat /tmp/au-1.8.0_20.xml | awk -F " /enclosure/'{print $(NF-1)}'`
    onlineversion2=${onlineversion1:50:8}
    onlineversionmain=${onlineversion2:2:1}
    echo "Online main: $onlineversionmain"
    onlineversionmin=${onlineversion2:6:2}
    echo "Online minor: $onlineversionmin"
    if [ "$onlineversionmain" -gt "$currentvermain" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Use the XML address defined in the OracleUpdateXML variable to query Oracle via curl 
# for the complete address of the latest Oracle Java 8 installer disk image.


fileURL=`/usr/bin/curl --silent $OracleUpdateXML | awk -F " /enclosure/'{print $(NF-1)}'`

# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi
if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 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_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /tmp/java_eight.XXXX`

        # Mount the latest Oracle Java 8 disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package. This installer may
        # be stored inside an install application on the disk image, or there
        # may be an installer package available at the root of the mounted disk
        # image.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *.app ))/Contents/Resources/*Java*.pkg" ]]; then    
            pkg_path="$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *.app ))/Contents/Resources/*Java*.pkg"
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *Java*.pkg -o -iname *Java*.mpkg ))" ]]; then    
            pkg_path="$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *Java*.pkg -o -iname *Java*.mpkg ))"
        fi

        # Before installation, the installer's developer certificate is checked to
        # see if it has been signed by Oracle's developer certificate. Once the 
        # certificate check has been passed, the package is then installed.

        if [[ "${pkg_path}" != "" ]]; then
            signature_check=`/usr/sbin/pkgutil --check-signature "$pkg_path" | awk /'Developer ID Installer/{ print $5 }'`
            if [[ ${signature_check} = "Oracle" ]]; then
                # Install Oracle Java 8 from the installer package stored inside the disk image
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1
            fi
        fi

        # Clean-up

        # Unmount the Oracle Java 8 disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi
34 REPLIES 34

gachowski
Valued Contributor II

Thanks Peter !!! :) not re-inventing the wheel is always great!!!!

C

dshumatetaos
New Contributor

Hi Peter,
Am trying this - sometimes it shows it runs all the way through but nothing is updated and other times I get an error (see below) any ideas?

Executing Policy Autoupdate Java (Test)...
Running script Autoupdate Java...
Script exit code: 1
Script result: Current version is Java 8 Update 31
Installed main version is 8
Installed minor version is 31
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1160 100 1160 0 0 3501 0 --:--:-- --:--:-- --:--:-- 3504
Online main: 8
Online minor: 73
Let's install Java! Main online version is equal than installed version, but minor version is higher.
Start installing Java
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 915 0 915 0 0 4145 0 --:--:-- --:--:-- --:--:-- 4140
100 935 0 935 0 0 3508 0 --:--:-- --:--:-- --:--:-- 3501

2 64.2M 2 1551k 0 0 1402k 0 0:00:46 0:00:01 0:00:45 1402k
6 64.2M 6 4064k 0 0 1928k 0 0:00:34 0:00:02 0:00:32 2507k
8 64.2M 8 5626k 0 0 1802k 0 0:00:36 0:00:03 0:00:33 2021k
11 64.2M 11 7261k 0 0 1769k 0 0:00:37 0:00:04 0:00:33 1904k
13 64.2M 13 9190k 0 0 1800k 0 0:00:36 0:00:05 0:00:31 1909k
17 64.2M 17 11.3M 0 0 1912k 0 0:00:34 0:00:06 0:00:28 2024k
20 64.2M 20 13.3M 0 0 1923k 0 0:00:34 0:00:07 0:00:27 1920k
23 64.2M 23 15.2M 0 0 1919k 0 0:00:34 0:00:08 0:00:26 1991k
26 64.2M 26 17.2M 0 0 1941k 0 0:00:33 0:00:09 0:00:24 2083k
30 64.2M 30 19.3M 0 0 1961k 0 0:00:33 0:00:10 0:00:23 2126k
33 64.2M 33 21.4M 0 0 1974k 0 0:00:33 0:00:11 0:00:22 2049k
37 64.2M 37 23.9M 0 0 2024k 0 0:00:32 0:00:12 0:00:20 2169k
40 64.2M 40 26.3M 0 0 2057k 0 0:00:31 0:00:13 0:00:18 2281k
44 64.2M 44 28.6M 0 0 2077k 0 0:00:31 0:00:14 0:00:17 2323k
48 64.2M 48 30.9M 0 0 2096k 0 0:00:31 0:00:15 0:00:16 2369k
51 64.2M 51 33.1M 0 0 2107k 0 0:00:31 0:00:16 0:00:15 2403k
54 64.2M 54 35.1M 0 0 2104k 0 0:00:31 0:00:17 0:00:14 2297k
59 64.2M 59 38.2M 0 0 2164k 0 0:00:30 0:00:18 0:00:12 2444k
64 64.2M 64 41.5M 0 0 2229k 0 0:00:29 0:00:19 0:00:10 2657k
69 64.2M 69 44.5M 0 0 2268k 0 0:00:28 0:00:20 0:00:08 2788k
72 64.2M 72 46.6M 0 0 2263k 0 0:00:29 0:00:21 0:00:08 2766k
77 64.2M 77 49.5M 0 0 2295k 0 0:00:28 0:00:22 0:00:06 2947k
82 64.2M 82 52.9M 0 0 2347k 0 0:00:28 0:00:23 0:00:05 3009k
87 64.2M 87 56.4M 0 0 2399k 0 0:00:27 0:00:24 0:00:03 3052k
92 64.2M 92 59.6M 0 0 2434k 0 0:00:27 0:00:25 0:00:02 3101k
98 64.2M 98 63.4M 0 0 2487k 0 0:00:26 0:00:26 --:--:-- 3432k
100 64.2M 100 64.2M 0 0 2497k 0 0:00:26 0:00:26 --:--:-- 3558k
hdiutil: attach failed - No such file or directory

davidacland
Honored Contributor II
Honored Contributor II

Thanks, I was looking for this just the other day. Much appreciated. Saved me a pile of work!!

Wakko
Contributor II

Any update form dshumatetaos post. It fails to update, this is the results I'm getting.

Script result: Current version is Java 8 Update 73 build 02
Installed main version is 8
Installed minor version is 73
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1160 100 1160 0 0 6054 0 --:--:-- --:--:-- --:--:-- 6073
Online main: 8
Online minor: 77
Let's install Java! Main online version is equal than installed version, but minor version is higher.
Start installing Java
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 935 0 935 0 0 4529 0 --:--:-- --:--:-- --:--:-- 4538

4 64.2M 4 3201k 0 0 3838k 0 0:00:17 --:--:-- 0:00:17 3838k
15 64.2M 15 9.9M 0 0 5580k 0 0:00:11 0:00:01 0:00:10 7034k
24 64.2M 24 15.9M 0 0 5758k 0 0:00:11 0:00:02 0:00:09 6558k
34 64.2M 34 22.0M 0 0 5900k 0 0:00:11 0:00:03 0:00:08 6473k
44 64.2M 44 28.5M 0 0 6058k 0 0:00:10 0:00:04 0:00:06 6520k
54 64.2M 54 34.9M 0 0 6128k 0 0:00:10 0:00:05 0:00:05 6509k
64 64.2M 64 41.4M 0 0 6217k 0 0:00:10 0:00:06 0:00:04 6450k
74 64.2M 74 47.6M 0 0 6234k 0 0:00:10 0:00:07 0:00:03 6503k
83 64.2M 83 53.4M 0 0 6200k 0 0:00:10 0:00:08 0:00:02 6430k
92 64.2M 92 59.6M 0 0 6213k 0 0:00:10 0:00:09 0:00:01 6362k
100 64.2M 100 64.2M 0 0 6249k 0 0:00:10 0:00:10 --:--:-- 6398k
/dev/disk4 GUID_partition_scheme /dev/disk4s1 Apple_HFS /private/tmp/java_eight.EJ9s
"disk4" unmounted.
"disk4" ejected.

peterlbk
Contributor

Hi All, and especially @dshumatetaos and @Echevarria just tested it again with this script and all worked well. Please let me know if this one still gives errors.

#!/bin/sh -x
# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=${currentver:5:1}
    echo "Installed main version is $currentvermain"
    currentvermin=${currentver:14:2}
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin1=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $6}'`
    onlineversionmin=${onlineversionmin1:0:2}
    echo "Online minor: $onlineversionmin"
    if [ -z "$currentvermain" ] || [ "$onlineversionmain" -gt "$currentvermain" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`curl http://www.java.com/en/download/manual.jsp | grep "Download Java for Mac OS X" | awk -F """ '{ print $4;exit}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi
if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then
        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /tmp/java_eight.XXXX`

        # Mount the latest Oracle Java 8 disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Find the Java installer

        JAVAVOL=`ls /Volumes/ | grep "Java"`
          JAVAAPP=`ls "$TMPMOUNT" | grep "Java"`
          pkg_path=`ls "$TMPMOUNT"/"$JAVAAPP"/Contents/Resources/*Java*.pkg`

        # Check installer certificate

        if [[ "${pkg_path}" != "" ]]; then
            signature_check=`/usr/sbin/pkgutil --check-signature "$pkg_path" | awk /'Developer ID Installer/{ print $5 }'`
            if [[ ${signature_check} = "Oracle" ]]; then
                # Install Oracle Java 8 from the installer package stored inside the disk image
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1
            fi
        fi

        # Clean-up

        # Unmount the Oracle Java 8 disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

    fi
fi

ryan_s
New Contributor II

Hey @peterloobuyck -- Same issue here as @dshumatetaos and @Echevarria ... I have tried both script versions you have on this site; it seems to try and do "stuff" but the version does not change after the script runs:

Checking for policies triggered by "java" for user "ryan.s"...
Executing Policy Java (JRE) Updater
Mounting NA-US-CA-Fremont-Office to /Volumes/casper...
Getting package details from https://jss.teslamotors.com:8443/...
Uninstalling...
    Looking for Applications...
    Deleting files...
Uninstalled oracle_java_jre_7u67.dmg.
Getting package details from https://jss.teslamotors.com:8443/...
Uninstalling...
    Looking for Applications...
    Deleting files...
Uninstalled oracle_java_jre_8u65.dmg.
Running script App_JavaJRE-autoupd...
Script exit code: 0
Script result: ++ sw_vers -productVersion
++ awk -F. '{print $2}'
+ osvers=11
+ plugin='/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist'
+ '[' -f '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist' ']'
+ echo 'No java installed, let'''s install'
No java installed, let's install
+ installjava=1
++ curl http://www.java.com/en/download/manual.jsp
++ grep 'Download Java for Mac OS X'
++ awk -F '"' '{ print $4;exit}'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 23151  100 23151    0     0   294k      0 --:--:-- --:--:-- --:--:--  297k
+ fileURL='http://javadl.oracle.com/webapps/download/AutoDL?BundleId=207766'
+ java_eight_dmg=/tmp/java_eight.dmg
+ [[ 11 -lt 7 ]]
+ '[' 1 = 1 ']'
+ echo 'Start installing Java'
Start installing Java
+ [[ 11 -ge 7 ]]
+ /usr/bin/curl --retry 3 -Lo /tmp/java_eight.dmg 'http://javadl.oracle.com/webapps/download/AutoDL?BundleId=207766'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   755    0   755    0     0   1233      0 --:--:-- --:--:-- --:--:--  1235
100 64.2M  100 64.2M    0     0  24.1M      0  0:00:02  0:00:02 --:--:-- 33.6M
++ /usr/bin/mktemp -d /tmp/java_eight.XXXX
+ TMPMOUNT=/tmp/java_eight.TjzG
+ hdiutil attach /tmp/java_eight.dmg -mountpoint /tmp/java_eight.TjzG -nobrowse -noverify -noautoopen
/dev/disk3              GUID_partition_scheme           
/dev/disk3s1            Apple_HFS                       /private/tmp/java_eight.TjzG
++ ls /Volumes/
++ grep Java
+ JAVAVOL=
++ ls /tmp/java_eight.TjzG
++ grep Java
+ JAVAAPP='Java 8 Update 91.app'
++ ls '/tmp/java_eight.TjzG/Java 8 Update 91.app/Contents/Resources/JavaAppletPlugin.pkg'
+ pkg_path='/tmp/java_eight.TjzG/Java 8 Update 91.app/Contents/Resources/JavaAppletPlugin.pkg'
+ [[ /tmp/java_eight.TjzG/Java 8 Update 91.app/Contents/Resources/JavaAppletPlugin.pkg != '' ]]
++ /usr/sbin/pkgutil --check-signature '/tmp/java_eight.TjzG/Java 8 Update 91.app/Contents/Resources/JavaAppletPlugin.pkg'
++ awk '/Developer ID Installer/{ print $5 }'
+ signature_check=Oracle
+ [[ Oracle = O
acle ]]
+ /usr/sbin/installer -dumplog -verbose -pkg '/tmp/java_eight.TjzG/Java 8 Update 91.app/Contents/Resources/JavaAppletPlugin.pkg' -target /
+ /usr/bin/hdiutil detach -force /tmp/java_eight.TjzG
"disk3" unmounted.
"disk3" ejected.
+ /bin/rm -rf /tmp/java_eight.TjzG
+ /bin/rm -rf /tmp/java_eight.dmg

Unmounting file server...

Looking at my Casper inventory: Java 8 Update 60.app Java 8 Update 60 build 27 /Users/ryan.s/Library/Application Support/Java/Java 1.8.60.27/Java 8 Update 60.app No

I'm trying to debug, can I cannot seem to find anything broken...

peterlbk
Contributor

Hi guys, @ryan.s @davidacland @Echevarria

Seems you're right, Oracle played with its own installer app.

Can you test the script underneath? I made some changes and tests in my testlab seems to be ok...

#!/bin/bash -e

# This script downloads and installs the latest Oracle Java 8 for compatible Macs

# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

OracleUpdateXML="https://javadl-esd-secure.oracle.com/update/mac/au-1.8.0_20.xml"

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=${currentver:5:1}
    echo "Installed main version is $currentvermain"
    currentvermin=${currentver:14:2}
    echo "Installed minor version is $currentvermin"
    /usr/bin/curl --retry 3 -Lo /tmp/au-1.8.0_20.xml $OracleUpdateXML
    onlineversion1=`cat /tmp/au-1.8.0_20.xml | awk -F " /enclosure/'{print $(NF-1)}'`
    onlineversion2=${onlineversion1:50:8}
    onlineversionmain=${onlineversion2:2:1}
    echo "Online main: $onlineversionmain"
    onlineversionmin=${onlineversion2:6:2}
    echo "Online minor: $onlineversionmin"
    if [ "$onlineversionmain" -gt "$currentvermain" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Use the XML address defined in the OracleUpdateXML variable to query Oracle via curl 
# for the complete address of the latest Oracle Java 8 installer disk image.


fileURL=`/usr/bin/curl --silent $OracleUpdateXML | awk -F " /enclosure/'{print $(NF-1)}'`

# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi
if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 software disk image

        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /Volumes/java_eight.XXXX`

        # Mount the latest Oracle Java disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.pkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.pkg`
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.mpkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.mpkg`
        fi

        # Before installation, the installer's developer certificate is checked 

        if [[ "${pkg_path}" != "" ]]; then
                echo "installing Java from ${pkg_path}..."
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1

        fi

        # Clean-up

        # Unmount the disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi

davidacland
Honored Contributor II
Honored Contributor II

Thanks, works ok for me.

bbot
Contributor

..

bbot
Contributor

The current script is erroring ever since Oracle released a 3 digit version number.

The $onlineversionmin variable is returning 10 instead of 101. $onlineversion2 also returning 1.8.0_10 instead of 1.8.0_101

While using variables like the below work for two digit version numbers, they break when Oracle released version 101. onlineversion2=${onlineversion1:50:9} onlineversionmain=${onlineversion2:2:1}

I'm making some modifications on the script to allow support for both two and three digit version numbers.
I changed it so that if Java isn't installed, it exits. (we didn't want users in our environment to get java installed if they didn't have it).

Also added functionality to remove the /tmp/au-1.8.0_20.xml once the script ran regardless if it installs or not. Previously it removed if the software needed to be updated.

#!/bin/bash

# This script downloads and installs the latest Oracle Java 8 for Macs

# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

OracleUpdateXML="https://javadl-esd-secure.oracle.com/update/mac/au-1.8.0_20.xml"

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"

    currentvermain=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString | awk '{print $2}'`
    echo "Installed main version is $currentvermain"

    currentvermin=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString | awk '{print $4}'`
    echo "Installed minor version is $currentvermin"

    /usr/bin/curl --retry 3 -Lo /tmp/au-1.8.0_20.xml $OracleUpdateXML

    onlineversion1=`cat /tmp/au-1.8.0_20.xml | awk -F " /enclosure/'{print $(NF-1)}' | grep -oE 'd.d.{1,2}d_d{1,3}'`  

    onlineversionmain=`cat /tmp/au-1.8.0_20.xml | awk -F " /enclosure/'{print $(NF-1)}' | grep -oE 'd.d.{1,2}d_d{1,3}' | cut -d '.' -f2`

    echo "Online main: $onlineversionmain"
    onlineversionmin=`cat /tmp/au-1.8.0_20.xml | awk -F " /enclosure/'{print $(NF-1)}' | grep -oE 'd.d.{1,2}d_d{1,3}' | cut -d "_" -f 2`
    echo "Online minor: $onlineversionmin"


    if [ "$onlineversionmain" -gt "$currentvermain" ]
    then
        echo "Java version is newer than installed version."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]
    then
        echo "Java is up-to-date!"
        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
else
    echo "No java installed, exit"
    installjava=0
    # Remove xml file
    /bin/rm -rf /tmp/au-1.8.0_20.xml
fi


# Use the XML address defined in the OracleUpdateXML variable to query Oracle via curl 
# for the complete address of the latest Oracle Java 8 installer disk image.


fileURL=`/usr/bin/curl --silent $OracleUpdateXML | awk -F " /enclosure/'{print $(NF-1)}'`

# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi
if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 software disk image

        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /Volumes/java_eight.XXXX`

        # Mount the latest Oracle Java disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.pkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.pkg`
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.mpkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.mpkg`
        fi

        # Before installation, the installer's developer certificate is checked 

        if [[ "${pkg_path}" != "" ]]; then
                echo "installing Java from ${pkg_path}..."
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1

        fi

        # Clean-up

        # Unmount the disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi

peterlbk
Contributor

Hi guys @bbot , since Oracle changes things a lot, I changed the script a bit. I now read the manual download page and grab versions. New script underneath

#!/bin/sh
# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=${currentver:5:1}
    echo "Installed main version is $currentvermain"
    currentvermin=${currentver:14:2}
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin1=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $6}' | awk -F "<" '{ print $1}'`
    onlineversionmin=${onlineversionmin1:0:3}
    echo "Online minor: $onlineversionmin"
    if [ -z "$currentvermain" ] || [ "$onlineversionmain" -gt "$currentvermain" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`curl http://www.java.com/en/download/manual.jsp | grep "Download Java for Mac OS X" | awk -F """ '{ print $4;exit}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi

cwaldrip
Valued Contributor

No chance you've got something similar for the Java JDK? ;-)

bjones
New Contributor III

@peterloobuyck

Hey buddy i wanted to see if any of the Java update scripts listed were able to address the issue with the policy completing successfully but the version is still the same. I ran this script and the following results took place but the update didn't happen:

Executing Policy Java Installation update
Running script New Java Update Script...
Script exit code: 0
Script result: Current version is Java 8 Update 73 build 02
Installed main version is 8
Installed minor version is 73
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1167 100 1167 0 0 2111 0 --:--:-- --:--:-- --:--:-- 2110
Online main: 8
Online minor: 10

bbot
Contributor

@bjones If you look at my post above, the issue is that the script isn't finding the new version number 101. As you can see in your log, it thinks the version number is Main 8, Minor 10, which is why the script is exiting.

See below in bold for what it should return.

Script result: Current version is Java 8 Update 71 build 15
Installed main version is 8
Installed minor version is 71
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1167 100 1167 0 0 5926 0 --:--:-- --:--:-- --:--:-- 5954
Online main: 8
Online minor: 101
Let's install Java! Main online version is equal than installed version, but minor version is higher.
Start installing Java
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 945 0 945 0 0 5708 0 --:--:-- --:--:-- --:--:-- 5762

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
7 64.3M 7 4753k 0 0 4035k 0 0:00:16 0:00:01 0:00:15 4758k
23 64.3M 23 14.9M 0 0 7008k 0 0:00:09 0:00:02 0:00:07 7638k
40 64.3M 40 26.1M 0 0 8417k 0 0:00:07 0:00:03 0:00:04 8921k
58 64.3M 58 37.3M 0 0 9152k 0 0:00:07 0:00:04 0:00:03 9564k
73 64.3M 73 47.0M 0 0 9313k 0 0:00:07 0:00:05 0:00:02 9648k
90 64.3M 90 58.3M 0 0 9666k 0 0:00:06 0:00:06 --:--:-- 10.7M
100 64.3M 100 64.3M 0 0 9810k 0 0:00:06 0:00:06 --:--:-- 10.8M
/dev/disk2 GUID_partition_scheme
/dev/disk2s1 Apple_HFS /Volumes/java_eight.DTf1
installing Java from /Volumes/java_eight.DTf1/Java 8 Update 101.app/Contents/Resources/JavaAppletPlugin.pkg...
"disk2" unmounted.
"disk2" ejected.

Try running my modified script above, it handles both 2 and 3 digit version numbers.

@peterloobuyck Have you modified your script for the new 3 digit version numbers?

peterlbk
Contributor

@bjones @bbot Yep!

#!/bin/sh
# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=${currentver:5:1}
    echo "Installed main version is $currentvermain"
    currentvermin=${currentver:14:2}
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin1=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $6}' | awk -F "<" '{ print $1}'`
    onlineversionmin=${onlineversionmin1:0:3}
    echo "Online minor: $onlineversionmin"
    if [ -z "$currentvermain" ] || [ "$onlineversionmain" -gt "$currentvermain" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`curl http://www.java.com/en/download/manual.jsp | grep "Download Java for Mac OS X" | awk -F """ '{ print $4;exit}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi

Rocky
New Contributor III

Made minor modification to lines 11, 13, and 18 to grab the word rather than a specific number of characters. Should keep this from needing a change (or breaking) when version numbers increase or decrease in digits.

#!/bin/sh
# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=$(echo $currentver | awk '{ print $2}')
    echo "Installed main version is $currentvermain"
    currentvermin=$(echo $currentver | awk '{ print $4}')
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin1=`curl http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $6}' | awk -F "<" '{ print $1}'`
    onlineversionmin=$(echo $onlineversionmin1 | awk '{ print $1}')
    echo "Online minor: $onlineversionmin"
    if [ -z "$currentvermain" ] || [ "$onlineversionmain" -gt "$currentvermain" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`curl http://www.java.com/en/download/manual.jsp | grep "Download Java for Mac OS X" | awk -F """ '{ print $4;exit}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi
if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 software disk image

        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /Volumes/java_eight.XXXX`

        # Mount the latest Oracle Java disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.pkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.pkg`
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.mpkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.mpkg`
        fi

        # Before installation, the installer's developer certificate is checked 

        if [[ "${pkg_path}" != "" ]]; then
                echo "installing Java from ${pkg_path}..."
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1

        fi

        # Clean-up

        # Unmount the disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi

cwaldrip
Valued Contributor

@Rocky I'm getting an error in line 20 when it tries to get/use (?) the minor version.

Online minor: 
./test.sh: line 20: [: : integer expression expected

I'm running Java 8 Update 111 build 14 on my test machine.

 Current version is Java 8 Update 111 build 14
Installed main version is 8
Installed minor version is 111

peterlbk
Contributor

@cwaldrip and @Rocky

Did some cleaning up. You should be able to use this one... Oracle did some changes on their website...

#!/bin/sh
# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=${currentver:5:1}
    echo "Installed main version is $currentvermain"
    currentvermin=${currentver:14:3}
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`curl -L http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin1=`curl -L http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $6}' | awk -F "<" '{ print $1}'`
    onlineversionmin=${onlineversionmin1:0:3}
    echo "Online minor: $onlineversionmin"
    if [ -z "${currentvermain}" ] || [ "${onlineversionmain}" -gt "${currentvermain}" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" -gt "${currentvermin}" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" = "${currentvermin}" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`curl -L http://www.java.com/en/download/manual.jsp | grep "Download Java for Mac OS X" | awk -F """ '{ print $4;exit}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi

if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 software disk image

        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /Volumes/java_eight.XXXX`

        # Mount the latest Oracle Java disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.pkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.pkg`
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.mpkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.mpkg`
        fi

        # Before installation, the installer's developer certificate is checked 

        if [[ "${pkg_path}" != "" ]]; then
                echo "installing Java from ${pkg_path}..."
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1

        fi

        # Clean-up

        # Unmount the disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi

cwaldrip
Valued Contributor

Yep, that did it. Thanks @peterloobuyck !

stevenjklein
Contributor II

Hmm, I tried the script posted on 11/8/16, and got this:

Executing Policy Install Java
Running script Install Java.sh...
Script exit code: 0
Script result: No java installed, let's install
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0
100 36 100 36 0 0 9 0 0:00:04 0:00:03 0:00:01 16
Start installing Java
curl: (3) malformed
hdiutil: attach failed - No such file or directory
hdiutil: detach failed - No such file or directory

Any suggestions?

peterlbk
Contributor

@stevenjklein Hi Steven, just ran another test and all runs smooth..

What version of MacOS did you run it on?
And just to be sure, I attach the latest version once more.

#!/bin/sh
# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=${currentver:5:1}
    echo "Installed main version is $currentvermain"
    currentvermin=${currentver:14:3}
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`curl -L http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin1=`curl -L http://www.java.com/en/download/manual.jsp | grep "Recommended Version" | awk '{ print $6}' | awk -F "<" '{ print $1}'`
    onlineversionmin=${onlineversionmin1:0:3}
    echo "Online minor: $onlineversionmin"
    if [ -z "${currentvermain}" ] || [ "${onlineversionmain}" -gt "${currentvermain}" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" -gt "${currentvermin}" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" = "${currentvermin}" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`curl -L http://www.java.com/en/download/manual.jsp | grep "Download Java for Mac OS X" | awk -F """ '{ print $4;exit}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi

if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 software disk image

        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /Volumes/java_eight.XXXX`

        # Mount the latest Oracle Java disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.pkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.pkg`
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.mpkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.mpkg`
        fi

        # Before installation, the installer's developer certificate is checked 

        if [[ "${pkg_path}" != "" ]]; then
                echo "installing Java from ${pkg_path}..."
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1

        fi

        # Clean-up

        # Unmount the disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi

stevenjklein
Contributor II

@peterloobuyck : Hi Peter. The target machine is running 10.12.2. I note that Oracle released a Java update in the last day or two, but I don't know if that's relevant.

peterlbk
Contributor

@stevenjklein hi steven, i tested it on a similar machine. It should work even after the update. You're not behind a proxy are you?

stevenjklein
Contributor II

@peterloobuyck : Okay, I decided to save it as a shell script, make it executable, and run it on the target machine.

It refused to run.

I checked, and discovered it had Mac line breaks (CR instead of LF). (I know, rookie mistake!)

I used this nifty bit of PERL to fix it:

perl -pi -e 's/
/
/g' install_Java.sh

And now it works.

Thanks for your help!

peterlbk
Contributor

@stevenjklein Hi Steven, glad it's all working now!

pmex
New Contributor

Great script!

I've made the following modifications to my copy of the script.

The main reason was to only download the manual.jsp file once, but also make it work on clients that hasn't been updated in a long time (and therefore only have two letters for the currentvermin string).

#!/bin/sh

# https://www.jamf.com/jamf-nation/discussions/17222/autoupdate-java-by-shell

# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# Download the jsp file for extrating version and url for the dmg
oracle_jsp=/tmp/oracle_manual.jsp
curl -L http://www.java.com/en/download/manual.jsp > $oracle_jsp

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=`echo "$currentver" | awk '{print$2}'`
    echo "Installed main version is $currentvermain"
    currentvermin=`echo "$currentver" | awk '{print$4}'`
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`cat < $oracle_jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin=`cat < $oracle_jsp | grep "Recommended Version" | awk '{ print $6}' | awk -F "<" '{ print $1}'`
    echo "Online minor: $onlineversionmin"
    if [ -z "${currentvermain}" ] || [ "${onlineversionmain}" -gt "${currentvermain}" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" -gt "${currentvermin}" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" = "${currentvermin}" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`cat < $oracle_jsp | grep "Download Java for Mac OS X" | head -1 | awk -F """ '{ print $4}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi

if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 software disk image

        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /Volumes/java_eight.XXXX`

        # Mount the latest Oracle Java disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.pkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.pkg`
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.mpkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.mpkg`
        fi

        # Before installation, the installer's developer certificate is checked 

        if [[ "${pkg_path}" != "" ]]; then
                echo "installing Java from ${pkg_path}..."
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1

        fi

        # Clean-up

        # Unmount the disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi
# Remove the jsp file
rm $oracle_jsp

edit: added the " | head -1 " suggested by @jpiperbe, since this seems to still be the latest version of the script

jpiperbe
New Contributor

@pmex I had to add a

| head -1

between the grep and awk on line 49, for some reason the jsp had "Download for Mac OS X" twice so there were two URLS. (May have been a recent change) but other than that, worked great.

jwojda
Valued Contributor II

is this for Java or the JDK?

pmex
New Contributor

@jpiperbe I fond that out as well. Unfourtunately I didn't see your post until I went here today to correct myself ;-)

JayDuff
Contributor II

I am running the latest version of the script (2/22/17 at 8:53 AM by @pmex ) with @jpiperbe 's | head -1 fix.

The script runs, and I am pretty sure it's installing, as the next time I run, it shows java installed as 8 U 131, which matches the latest version, so it quits. However, when I go into Terminal, and type java --version, it says I have no JRE.

So, looking at the script, it seems like this is updating the Internet Plug-in. Indeed, Safari sees and recognizes it.

So, how can I modify this script to install the actual JRE? We use PowerSchool, and the gradebook requires a JRE.

Thanks for this great script!

herbiehandal
New Contributor

@JayDuff Its been a while since you asked the question, but here it goes anyways. The results you are getting from your script seem to be accurate. Running java --version in terminal look for the JSE and will try to install if its not found. This script is dealing with Java JRE, not JSE.

You can check the current version via System Preferences or Safari > Preferences > Websites > Plugins.

dmuncey
New Contributor II

Thank you for this script. I've been using for several months now and it has been a huge help with updating/installing the Java JRE in our environment.

I am running into a (hopefully) simple issue, however. The script is running flawlessly, as written, except it doesn't currently update to the current Patch Management version. Currently, the JRE version in Patch Management in the JSS is 8u162, but the manual download page is currently recommending 8u161. Running the script does upgrade all of our users to 8u161, but we still show are 0% patched since Patch Management is looking for 8u162.

We rely on the Patch Management reports to generate our monthly reports. These reports are presented to upper management, so I'd like to find a way to not need to document exceptions each month.

Are there others who are experiencing this? How have you solved the issue?

techjason
Contributor

@dmuncey I have seen the same issue and it is still happening now, a month after your post. I am not sure why this is happening the newest version released by Oracle is 161.

robinsonjo
Contributor
Contributor

Hello @dmuncey and @techjason ,

We recently released two Software Titles specifically for the CPU release of Java 8. CPU meaning critical patch update and that it follows Oracle's recommended security update path.

The Software titles are named: Java SE Runtime Environment JRE8 CPU
Java SE Development Kit 8 CPU

The other Software Titles contain both the CPU and PSU updates and that's why it shows the PSU version of .162 as the latest. The above mentioned will show .161 as the latest.

For more information about the difference between PSU and CPU versions please reference: http://www.oracle.com/technetwork/java/javase/cpu-psu-explained-2331472.html

Hope this helps!

Jonny