Skip to content

Instantly share code, notes, and snippets.

@n0ts
Last active September 16, 2023 12:07
Show Gist options
  • Save n0ts/40dd9bd45578556f93e7 to your computer and use it in GitHub Desktop.
Save n0ts/40dd9bd45578556f93e7 to your computer and use it in GitHub Desktop.
Get latest Oracle JDK package bash shell script for linux/osx/windows
#!/bin/bash
# You must accept the Oracle JDK License Update
# https://www.oracle.com/java/technologies/javase-downloads.html
# usage: get_oracle_jdk_x64.sh <jdk_version> <platform> <ext>
# jdk_version: 14
# platform: linux or osx or windows
# ext: rpm or dmg or tar.gz or exec
jdk_version=${1:-14}
platform=${2:-linux}
ext=${3:-rpm}
readonly url="https://www.oracle.com"
readonly jdk_download_url1="$url/java/technologies/javase-downloads.html"
readonly jdk_download_url2=$(
curl -s $jdk_download_url1 | \
egrep -o "/\java\/technologies\/javase-jdk${jdk_version}-downloads.html"
)
[[ -z "$jdk_download_url2" ]] && echo "Could not get jdk download url - $jdk_download_url1" >> /dev/stderr
readonly jdk_download_url3="${url}${jdk_download_url2}"
readonly jdk_download_url4=$(
curl -s $jdk_download_url3 | \
egrep -o "download.oracle\.com\/otn-pub\/java\/jdk\/(${jdk_version})\..*\/.*\/jdk-${jdk_version}\..*[_]${platform}-x64_bin.${ext}\'" | tr -d \'
)
for dl_url in ${jdk_download_url4[@]}; do
wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
-N $dl_url
done
@fabiodbr
Copy link

JDK_DOWNLOAD_URL2=curl -s $JDK_DOWNLOAD_URL1 | egrep -o "\/technetwork\/java/\javase\/downloads\/jdk${JDK_VERSION}-downloads-.+?\.html" | head -1 | awk '{match($1,"/technetwork/java/javase/downloads/jdk[0-9]-downloads-.+?.html",a)}END{print a[0]}'

@ryenus
Copy link

ryenus commented Aug 22, 2016

Another nice guy has made a install script:
https://github.com/AdamScheller/UbuntuJavaInstaller

@RobertTheNerd
Copy link

RobertTheNerd commented Aug 26, 2016

Very good job!

In my case, I always choose to install the latest one instead of multiple ones. Hence adding a | tail -1 to the end of JDK_DOWNLOAD_URL4:

JDK_DOWNLOAD_URL4=`curl -s $JDK_DOWNLOAD_URL3 | egrep -o "http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[7-8]u[0-9]+\-(.*)+\/jdk-[7-8]u[0-9]+(.*)linux-x64.${EXT}" | tail -1`

@VGerris
Copy link

VGerris commented Sep 9, 2016

exactly what I was looking for, thank you. I incorporated the changes from the commenters, works great.

@n0ts
Copy link
Author

n0ts commented Oct 19, 2016

Hi, thanks you for comments.
I updated get_jdk.sh.
Thanks.

@cyborch
Copy link

cyborch commented Feb 28, 2017

Thank you! This is very useful!

There is a small typo in line 14 which should say ext="tar.gz" instead of exit="tar.gz".

@i47i
Copy link

i47i commented Mar 14, 2017

Thank you, it works perfectly.

@indranil-chakraborty
Copy link

Seconded @cyborch, line 14 needs to specify ext instead of exit

@n0ts
Copy link
Author

n0ts commented Jun 9, 2017

There is a small typo in line 14 which should say ext="tar.gz" instead of exit="tar.gz".

Thanks, I fix it.

@aankittcoolest
Copy link

Thanks a lot :)

@daxprime
Copy link

Triplle thanks dear sir, Works like a charm

@n0ts
Copy link
Author

n0ts commented Sep 22, 2017

Hello, I support JDK new version 9!!!

@moriyoshi
Copy link

This really saved my life. Thanks!

@andrewkro
Copy link

I used this as well, really nice and easy script.

oracle now changed this to use versioning in the name so this stopped working - i've fixed it by changing the regex

-    egrep -o "http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[8-9](u[0-9]+|\+).*\/jdk-${jdk_version}.*(-|_)linux-(x64|x64_bin).$ext"
+    egrep -o "http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[8-9](u[0-9]+|\+|[.0-9]+\+).*\/jdk-${jdk_version}.*(-|_)linux-(x64|x64_bin).$ext"

Or specifically,

from:
(u[0-9]+|\+)
to:
(u[0-9]+|\+|[.0-9]+\+)

@omanpls
Copy link

omanpls commented Nov 7, 2017

working on Ubuntu 16.04 with andrewkro fix

@Jorpaes
Copy link

Jorpaes commented Nov 27, 2017

working on Ubuntu 16.04 with andrewkro fix

@imperialguy
Copy link

do you have a similar script for jre?

@tstibbs
Copy link

tstibbs commented Apr 10, 2018

Note the regex doesn't work for 10 - it's hard coded for java versions starting with 8 or 9. I rewrote it to cover 10 also:
http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/([1-9][\.u0-9]*)(\+|\-)[b0-9]*\/[a-f0-9]+\/jdk-\1(-|_)linux-(x64|x64_bin).$ext

I also added | sort -r | head -n 1 to the end of the url4 command to ensure that it grabs the latest version if there are multiple in the page.

@liedekef
Copy link

Since the most part of that script is done via curl, why not change the last wget to also curl:
curl -C - -LR#OH "Cookie: oraclelicense=accept-securebackup-cookie" -k $dl_url

@extremeshok
Copy link

extremeshok commented May 9, 2018

+1 on only using curl, note the above curl does not work

@swallace21
Copy link

@andrewkro

Tried your fix for new versions, does not seem to work. Is it still working for you?

@rajeshkp
Copy link

Thanks @liedekef it works

@jonathan-russo
Copy link

The script stopped working for me today. It appears the Oracle is now responding to these calls with a 301 response. Adding the -L flag to curl on lines 15 and 24 solved the issue for me.

@dasgoll
Copy link

dasgoll commented Oct 29, 2018

The script stopped working for me today. It appears the Oracle is now responding to these calls with a 301 response. Adding the -L flag to curl on lines 15 and 24 solved the issue for me.

Confirmed. Thanks @jonathan-russo

@Techwolf
Copy link

Techwolf commented Nov 2, 2018

I am getting "302 Moved Temporarily" and then "401 Authorization Required".

@jonathan-russo
Copy link

So this script has broken again on me. I have now decided to simply download the tar.gz files and put them on a local server for consistent access. Thanks for the original script @n0ts

@FRidh
Copy link

FRidh commented Dec 4, 2018

Seems to need https.

@alexws54tk
Copy link

JDK_DOWNLOAD_URL4 need https and change otn-pub to otn:

readonly jdk_download_url4=$(
    curl -s $jdk_download_url3 | \
    egrep -o "https\:\/\/download.oracle\.com\/otn\/java\/jdk\/[8-9](u[0-9]+|\+).*\/jdk-${jdk_version}.*(-|_)linux-(x64|x64_bin).$ext"
)

And now NEED oracle account login/passwd.

@umersajid59
Copy link

i soon i run the script I get this error "Could not get jdk download url - https://www.oracle.com/technetwork/java/javase/downloads/index.html"

@n0ts
Copy link
Author

n0ts commented Jun 1, 2020

i soon i run the script I get this error "Could not get jdk download url - https://www.oracle.com/technetwork/java/javase/downloads/index.html"

Hi, I updated get_oracle_jdk_x64.sh .

For Linux
./get_oracle_jdk_x64.sh 14 linux rpm

For macOS
./get_oracle_jdk_x64.sh 14 osx dmg

For Windows
./get_oracle_jdk_x64.sh 14 windows exe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment