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.
53 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" \ $URLGet 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 thedownloadsarea/\['files'\]/– We need thefilesarea/linux-x64/– The target architectureThat you could adapt.
/\.tar\.gz/– The file formatThat you could adapt.
If you do that, don't forget the
tar.gzingrep -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=""
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 0Useful
List all architectures and file formats with this command:
curl -s | \ 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":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-i586.tar.gz'] = { "title":"Linux x86", "size":"166.95 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-x64.rpm'] = { "title":"Linux x64", "size":"145.19 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-x64.tar.gz'] = { "title":"Linux x64", "size":"165.25 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-macosx-x64.dmg'] = { "title":"Mac OS X x64", "size":"222.09 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-sparcv9.tar.Z'] = { "title":"Solaris SPARC 64-bit (SVR4 package)", "size":"139.36 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-sparcv9.tar.gz'] = { "title":"Solaris SPARC 64-bit", "size":"98.8 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-x64.tar.Z'] = { "title":"Solaris x64 (SVR4 package)", "size":"139.79 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-x64.tar.gz'] = { "title":"Solaris x64", "size":"96.45 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-windows-i586.exe'] = { "title":"Windows x86", "size":"176.02 MB","filepath":""};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-windows-x64.exe'] = { "title":"Windows x64", "size":"180.51 MB","filepath":""}; 6 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 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
- 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 ; 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 filesuser@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 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 | \ 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"
fiTo 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.
7More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"