4

The popular WebUpd8 Team PPA is not being updated.

It has still obsolete versions like 8u45.

I manually downloaded JDK from Oracle site and replaced it.

Is there a way to automatically check Oracle java latest version and download it, if it has been updated?

I see the main problem is to download it. I looked in oracle-java8-installer scripts and see some weird links they download it from.

The tarball can't be downloaded directly from Oracle site without accepting a license. The installer checked that too. But I do not quite get how to accept the license from command line.

This script could be added to cron.

Just in case the PPA is never updated, or if there are delays like this time.

5
  • Hmm, looks interesting =)
    – A.B.
    Jul 23, 2015 at 14:21
  • you can download it by accepting the license when providing special cookie. This can be passed as additional parameter to wget/curl
    – ibre5041
    Jul 23, 2015 at 14:50
  • @ibre5041 Can you make at least a download script? But I can't find a permanent download link.
    – Pilot6
    Jul 23, 2015 at 14:53
  • One question: would a script that checks the latest downloaded version (downloaded from the script itself) be enough? I.e.: a script that will just download the latest version the first time and that won't download anything more until a new version it's released
    – kos
    Jul 26, 2015 at 12:47
  • @kos I check java version. Look at my answer pls.
    – Pilot6
    Jul 26, 2015 at 13:23

3 Answers 3

7

The script works in conjunction with the WebUpd8 PPA. Therefore install Java by the PPA first.

If there is a new version in the PPA, then this is installed via the package manager. In the meantime, the script can install a newer version, it there is a new version on the Oracle website.


A few considerations

Download a version via wget, eg:

wget --no-cookies --header \
    "Cookie: oraclelicense=accept-securebackup-cookie" \
    $URL

Get the URL with (for linux-x64 and tar.gz)

URL=$(curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
    awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /linux-x64/ && /\.tar\.gz/" | \
    grep -o 'http.*\.tar\.gz')

All in one

for Oracle Java 8 via the URL http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
    $(curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
        awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /linux-x64/ && /\.tar\.gz/" | \
        grep -o 'http.*\.tar\.gz'\
    )

Explanation

  • /downloads\['/ – We need the downloads area

  • /\['files'\]/ – We need the files area

  • /linux-x64/ – The target architecture

    That you could adapt.

  • /\.tar\.gz/ – The file format

    That you could adapt.

    If you do that, don't forget the tar.gz in grep -o 'http.*\.tar\.gz'

  • ! /demos/ – No demos


The script for Oracle Java 8, x64, tar.gz

#!/bin/bash
major_version="8"
target="/usr/lib/jvm/java-8-oracle"
arch="linux-x64"

# This URL is for the major version 8.
download_page="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html"

download_url=$(curl -s "$download_page" | \
        awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /$arch/ && /\.tar\.gz/" | \
        grep -o 'http.*\.tar\.gz')

server=$(awk -Fjdk-"$major_version"u '{split($2,a,/-/); print a[1]}' <<< "$download_url")
local=$("$target"/bin/java -version 2>&1 | awk -F_ '/java version/ {gsub(/"/,""); print $2}')

[ "$local" == "" ] && local=0

filename=$(awk -F'\/' '{print $NF}' <<< "$download_url")

[ "$server" -gt "$local" ] && wget -q -P "/tmp" --no-cookies --show-progress --header \
    "Cookie: oraclelicense=accept-securebackup-cookie" \
    "$download_url" 

if [ -f "$filename" ]; then
    mkdir -p "$target"
    tar -xf "$filename" --strip 1 -C "$target"
fi

exit 0

Useful

List all architectures and file formats with this command:

curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
        awk "/downloads\['/ && ! /demos/ && /\['files'\]\['jdk/"

Sample output

downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-i586.rpm'] = { "title":"Linux x86", "size":"146.9 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-i586.rpm"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-i586.tar.gz'] = { "title":"Linux x86", "size":"166.95 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-i586.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-x64.rpm'] = { "title":"Linux x64", "size":"145.19 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.rpm"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-x64.tar.gz'] = { "title":"Linux x64", "size":"165.25 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-macosx-x64.dmg'] = { "title":"Mac OS X x64", "size":"222.09 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-macosx-x64.dmg"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-sparcv9.tar.Z'] = { "title":"Solaris SPARC 64-bit (SVR4 package)", "size":"139.36 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-sparcv9.tar.Z"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-sparcv9.tar.gz'] = { "title":"Solaris SPARC 64-bit", "size":"98.8 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-sparcv9.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-x64.tar.Z'] = { "title":"Solaris x64 (SVR4 package)", "size":"139.79 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-x64.tar.Z"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-x64.tar.gz'] = { "title":"Solaris x64", "size":"96.45 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-x64.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-windows-i586.exe'] = { "title":"Windows x86", "size":"176.02 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-windows-i586.exe"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-windows-x64.exe'] = { "title":"Windows x64", "size":"180.51 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-windows-x64.exe"};
6
  • Thx. The next real problem is how to guess a link to the next release. The last one was ...8u45-b14.... Maybe test all `bnn?
    – Pilot6
    Jul 26, 2015 at 10:49
  • @Pilot6 I am still pondering on =\
    – A.B.
    Jul 26, 2015 at 10:53
  • That's cool! Now it is easy to write a script that checks current java version, compares it to the one available. If needed it will download a tar and install.
    – Pilot6
    Jul 26, 2015 at 11:31
  • You know what? I am not sure that the link jdk8-downloads-2133151.html will stay after they update the version.
    – Pilot6
    Jul 26, 2015 at 19:43
  • So the whole point does not make sense. They change it every time. Someone has to update link, like it is done in the ppa.
    – Pilot6
    Jul 26, 2015 at 19:55
0

This will automatically download the latest Oracle Java version for the currently running kernel's architecture, however as of now is missing the installation part (note: in the examples I've checked the current Java version against a custom text file in order to spoof the actual Java version)

JavaUpdater.sh

#!/bin/bash
echo 'Checking for a new Java Version...'
kern_arch=$(uname -r | sed 's/.*-\(.*\)/\1/')
if [ "$kern_arch" = amd64 ]; then
    java_ver=' x64'
fi
current_version=$(java -version 2>&1 >/dev/null | sed -n '1s/.*"\([^"]*\)"/\1/p')
latest_version_url=$(curl https://www.java.com/en/download/manual.jsp 2>/dev/null | grep -Po -m 1 "href=\"\K[^\"]*(?=.*Linux${java_ver} en JRE)"
)
latest_version=$(curl $latest_version_url 2>/dev/null | sed -n 's/.*File=jre-\([0-9]\)u\([0-9]\{2\}\).*/1.\1.0_\2/p')
if [ "$current_version" = "$latest_version" ]; then
    echo 'No new Java version available. Aborting.'
    exit 1
fi
read -n 1 -p "A new Java version is available (${latest_version})! Would you like to download it (y)? " download
echo
if [ "$download" = y ]; then
    filename="$(curl "$latest_version_url" 2> /dev/null | sed -n 's/.*File=\([^&]*\).*/\1/p')"
    wget -q -O "$filename" --show-progress "$latest_version_url"
    echo 'Download completed.'
    exit 0
fi
exit 1
  • Checks the currently running kernel's architecture in order to choose which Java version to check for updates and eventually download
  • Checks the current Java version against the latest Java version available for download at https://www.java.com/en/download/manual.jsp
  • Lets the user decide whether to download a new version or not

Caveats:

  • As of now, it assumes a single installation of Oracle Java
  • The script relies on the current structure of the page at https://www.java.com/en/download/manual.jsp; an update to the page's structure on Oracle's side might break it;
  • The script relies on the filename of the latest version available for download in order to extabilish the latest version; for example, the latest 64-bit version available for download, named jre-8u51-linux-x64.tar.gz, will be interpreted as Java 8 Update 51 (in terms of Java versions, 1.8.0_51);
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
└── JavaUpdater.sh

0 directories, 2 files
user@debian ~/tmp/JavaDownloader
 % cat current_version.txt 
java version "1.8.0_50"
Java(TM) SE Runtime Environment (build 1.8.0_50-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.50-b03, mixed mode)
user@debian ~/tmp/JavaDownloader
 % bash JavaUpdater.sh 
Checking for a new Java Version...
A new Java version is available (1.8.0_51)! Would you like to download it (y)? y
jre-8u51-linux-x64. 100%[=====================>]  60.90M  1.21MB/s   in 51s    
Download completed.
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
├── JavaUpdater.sh
└── jre-8u51-linux-x64.tar.gz

0 directories, 3 files
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
├── JavaUpdater.sh
└── jre-8u51-linux-x64.tar.gz

0 directories, 3 files
user@debian ~/tmp/JavaDownloader
 % cat current_version.txt 
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
user@debian ~/tmp/JavaDownloader
 % bash JavaUpdater.sh 
Checking for a new Java Version...
No new Java version available. Aborting.
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
├── JavaUpdater.sh
└── jre-8u51-linux-x64.tar.gz

0 directories, 3 files
3
  • java -version is a bad idea if you have multiple java versions installed and the default Java isn't Oracle Java.
    – A.B.
    Jul 26, 2015 at 15:16
  • sorry, here comes the smile :)
    – A.B.
    Jul 26, 2015 at 15:32
  • @A.B. Hey, no worries ;). Yes it does assume a single installation of Oracle Java, I've mentioned this in the "Caveats" section.
    – kos
    Jul 26, 2015 at 15:42
0

Here is the script based on @A.B. answer.

This script installs Oracle java 8 64-bit. On each run it checks version and updates java to the latest available.

It can be placed to cron.daily or cron.weekly, to check for updates.

#!/bin/sh

J_INSTALL_DIR=/usr/lib/jvm/java-8-oracle

URL=$(curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
    awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /linux-x64/ && /\.tar\.gz/" | \
    grep -o 'http.*\.tar\.gz') 

AVAILABLE=$(echo $URL | grep -o -P 'jdk-8u.{0,2}' | cut -d "u" -f 2)
CURRENT=$("$J_INSTALL_DIR"/bin/java -version 2>&1 | awk '/version/ {print $3}' | cut -d_ -f 2 | tr -d '"')

if [ -z $CURRENT ] || [ $AVAILABLE -gt $CURRENT ]; then
    cd /var/cache/oracle-jdk8-installer
    rm -f jdk-8u"$CURRENT"-linux-x64.tar.gz
    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" $URL
    tar -xzf jdk-8u"$AVAILABLE"-linux-x64.tar.gz
    rm -rf $J_INSTALL_DIR
    mv jdk1.8.0_"$AVAILABLE"/ $J_INSTALL_DIR

    LATEST=$(LANG=C update-alternatives --display java | grep ^/ | sed -e 's/.* //g' | sort -n | tail -1)
    if [ -z $LATEST ]; then
        LATEST=1
    else
        J_PATH=$(LANG=C update-alternatives --display java | grep "priority "$LATEST"" | awk '{print $1}')
        [ $J_PATH = "$J_INSTALL_DIR"/jre/bin/java ] || LATEST=$((LATEST+1))
    fi  

    #link JRE files
    for f in $J_INSTALL_DIR/jre/bin/*; do
        name=`basename $f`;
        if [ ! -f "/usr/bin/$name" -o -L "/usr/bin/$name" ]; then  #some files, like jvisualvm might not be links
            if [ -f "$J_INSTALL_DIR/man/man1/$name.1.gz" ]; then

                update-alternatives --install /usr/bin/$name $name $J_INSTALL_DIR/jre/bin/$name $LATEST --slave /usr/share/man/man1/$name.1.gz $name.1.gz $J_INSTALL_DIR/man/man1/$name.1.gz
            fi
        else     
                update-alternatives --install /usr/bin/$name $name $J_INSTALL_DIR/jre/bin/$name $LATEST

        fi
    done

    echo "Oracle Java 8 installed"

fi

if [ $AVAILABLE -eq $CURRENT ]; then
    echo "Java is up to date"
fi

To test it now, you can set CURRENT=45 in the script.

But some minor improvements can be done, like fontconfig.

The script can be downloaded from github too.

Pull requests and complaints are welcome.

7
  • wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" "$URL"
    – A.B.
    Jul 26, 2015 at 13:35
  • @A.B. I got it. Now some checks for existing files or dirs may be added.
    – Pilot6
    Jul 26, 2015 at 13:38
  • And also this can be done with update-alternatives to a fresh install without that deb from ppa.
    – Pilot6
    Jul 26, 2015 at 13:45
  • CURRENT=$(java -version 2>&1 | awk -F_ '/java version/ {gsub(/"/,""); print $2}')
    – A.B.
    Jul 26, 2015 at 13:46
  • @A.B. This looks better. But does same.
    – Pilot6
    Jul 26, 2015 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.