How to download and install Oracle Java by a script?
Solution 1
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.
Solution 2
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 thedownloads
area/\['files'\]/
– We need thefiles
area-
/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.gz
ingrep -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"};
Related videos on Youtube
Pilot6
Updated on September 18, 2022Comments
-
Pilot6 over 1 year
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.
-
A.B. almost 9 yearsHmm, looks interesting =)
-
ibre5041 almost 9 yearsyou can download it by accepting the license when providing special cookie. This can be passed as additional parameter to wget/curl
-
Pilot6 almost 9 years@ibre5041 Can you make at least a download script? But I can't find a permanent download link.
-
kos almost 9 yearsOne 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
-
Pilot6 almost 9 years@kos I check java version. Look at my answer pls.
-
-
Pilot6 almost 9 yearsThx. The next real problem is how to guess a link to the next release. The last one was
...8u45-b14...
. Maybe test all `bnn? -
A.B. almost 9 years@Pilot6 I am still pondering on =\
-
A.B. almost 9 years
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" "$URL"
-
Pilot6 almost 9 years@A.B. I got it. Now some checks for existing files or dirs may be added.
-
Pilot6 almost 9 yearsAnd also this can be done with
update-alternatives
to a fresh install without that deb from ppa. -
A.B. almost 9 years
CURRENT=$(java -version 2>&1 | awk -F_ '/java version/ {gsub(/"/,""); print $2}')
-
Pilot6 almost 9 years@A.B. This looks better. But does same.
-
Pilot6 almost 9 yearsLet us continue this discussion in chat.
-
A.B. almost 9 years
java -version
is a bad idea if you have multiple java versions installed and the default Java isn't Oracle Java. -
A.B. almost 9 yearssorry, here comes the smile :)
-
kos almost 9 years@A.B. Hey, no worries ;). Yes it does assume a single installation of Oracle Java, I've mentioned this in the "Caveats" section.
-
A.B. almost 9 yearsSomeone gave us downvotes. :(
-
Pilot6 almost 9 yearsYou know what? I am not sure that the link
jdk8-downloads-2133151.html
will stay after they update the version. -
Pilot6 almost 9 yearsSo the whole point does not make sense. They change it every time. Someone has to update link, like it is done in the ppa.
-
A.B. almost 9 years@Pilot6 A new link for each new version? o_O