How to Download and Install Oracle Java by a Script?

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

3 Answers

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  | \
    awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /linux-x64/ && /\.tar\.gz/" | \
    grep -o 'http.*\.tar\.gz')

All in one

for Oracle Java 8 via the URL

wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
    $(curl -s  | \
        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


Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.