Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get latest Oracle JDK package bash shell script
#!/bin/bash
# You must accept the Oracle Binary Code License
# http://www.oracle.com/technetwork/java/javase/terms/license/index.html
# usage: get_jdk.sh <jdk_version> <ext>
# jdk_version: 8(default) or 9
# ext: rpm or tar.gz
jdk_version=${1:-8}
ext=${2:-rpm}
readonly url="http://www.oracle.com"
readonly jdk_download_url1="$url/technetwork/java/javase/downloads/index.html"
readonly jdk_download_url2=$(
curl -s $jdk_download_url1 | \
egrep -o "\/technetwork\/java/\javase\/downloads\/jdk${jdk_version}-downloads-.+?\.html" | \
head -1 | \
cut -d '"' -f 1
)
[[ -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 "http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[8-9](u[0-9]+|\+).*\/jdk-${jdk_version}.*(-|_)linux-(x64|x64_bin).$ext"
)
for dl_url in ${jdk_download_url4[@]}; do
wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
-N $dl_url
done
@LordH3lmchen

This comment has been minimized.

Show comment
Hide comment
@LordH3lmchen

LordH3lmchen Apr 24, 2015

JDK_DOWNLOAD_URL2 is to greedy.

Lazy works better
JDK_DOWNLOAD_URL2=curl -s $JDK_DOWNLOAD_URL1 | grep -Po "\/technetwork\/java/\javase\/downloads\/jdk${JDK_VERSION}-downloads-.+?\.html" | head -1

LordH3lmchen commented Apr 24, 2015

JDK_DOWNLOAD_URL2 is to greedy.

Lazy works better
JDK_DOWNLOAD_URL2=curl -s $JDK_DOWNLOAD_URL1 | grep -Po "\/technetwork\/java/\javase\/downloads\/jdk${JDK_VERSION}-downloads-.+?\.html" | head -1

@jbjonesjr

This comment has been minimized.

Show comment
Hide comment
@jbjonesjr

jbjonesjr Feb 13, 2016

this script did not work without @LordH3lmchen update.

jbjonesjr commented Feb 13, 2016

this script did not work without @LordH3lmchen update.

@n0ts

This comment has been minimized.

Show comment
Hide comment
@n0ts

n0ts Mar 25, 2016

@LordH3lmchen @jbjonesjr
Thanks comment. I fix get_jdk.sh script.
Thanks!

Owner

n0ts commented Mar 25, 2016

@LordH3lmchen @jbjonesjr
Thanks comment. I fix get_jdk.sh script.
Thanks!

@fabiodbr

This comment has been minimized.

Show comment
Hide comment
@fabiodbr

fabiodbr May 12, 2016

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]}'

fabiodbr commented May 12, 2016

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

This comment has been minimized.

Show comment
Hide comment
@ryenus

ryenus Aug 22, 2016

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

ryenus commented Aug 22, 2016

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

@RobertTheNerd

This comment has been minimized.

Show comment
Hide comment
@RobertTheNerd

RobertTheNerd 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`

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

This comment has been minimized.

Show comment
Hide comment
@VGerris

VGerris Sep 9, 2016

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

VGerris commented Sep 9, 2016

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

@n0ts

This comment has been minimized.

Show comment
Hide comment
@n0ts

n0ts Oct 19, 2016

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

Owner

n0ts commented Oct 19, 2016

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

@cyborch

This comment has been minimized.

Show comment
Hide comment
@cyborch

cyborch 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".

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

This comment has been minimized.

Show comment
Hide comment
@i47i

i47i Mar 14, 2017

Thank you, it works perfectly.

i47i commented Mar 14, 2017

Thank you, it works perfectly.

@indranil-chakraborty

This comment has been minimized.

Show comment
Hide comment
@indranil-chakraborty

indranil-chakraborty May 17, 2017

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

indranil-chakraborty commented May 17, 2017

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

@n0ts

This comment has been minimized.

Show comment
Hide comment
@n0ts

n0ts 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.

Owner

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

This comment has been minimized.

Show comment
Hide comment
@aankittcoolest

aankittcoolest Jun 21, 2017

Thanks a lot :)

aankittcoolest commented Jun 21, 2017

Thanks a lot :)

@daxprime

This comment has been minimized.

Show comment
Hide comment
@daxprime

daxprime Sep 15, 2017

Triplle thanks dear sir, Works like a charm

daxprime commented Sep 15, 2017

Triplle thanks dear sir, Works like a charm

@n0ts

This comment has been minimized.

Show comment
Hide comment
@n0ts

n0ts Sep 22, 2017

Hello, I support JDK new version 9!!!

Owner

n0ts commented Sep 22, 2017

Hello, I support JDK new version 9!!!

@moriyoshi

This comment has been minimized.

Show comment
Hide comment
@moriyoshi

moriyoshi Oct 1, 2017

This really saved my life. Thanks!

moriyoshi commented Oct 1, 2017

This really saved my life. Thanks!

@andrewkro

This comment has been minimized.

Show comment
Hide comment
@andrewkro

andrewkro Oct 23, 2017

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]+\+)

andrewkro commented Oct 23, 2017

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

This comment has been minimized.

Show comment
Hide comment
@omanpls

omanpls Nov 7, 2017

working on Ubuntu 16.04 with andrewkro fix

omanpls commented Nov 7, 2017

working on Ubuntu 16.04 with andrewkro fix

@Jorpaes

This comment has been minimized.

Show comment
Hide comment
@Jorpaes

Jorpaes Nov 27, 2017

working on Ubuntu 16.04 with andrewkro fix

Jorpaes commented Nov 27, 2017

working on Ubuntu 16.04 with andrewkro fix

@imperialguy

This comment has been minimized.

Show comment
Hide comment
@imperialguy

imperialguy Jan 13, 2018

do you have a similar script for jre?

imperialguy commented Jan 13, 2018

do you have a similar script for jre?

@tstibbs

This comment has been minimized.

Show comment
Hide comment
@tstibbs

tstibbs 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.

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

This comment has been minimized.

Show comment
Hide comment
@liedekef

liedekef Apr 10, 2018

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

liedekef commented Apr 10, 2018

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

This comment has been minimized.

Show comment
Hide comment
@extremeshok

extremeshok May 9, 2018

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

extremeshok commented May 9, 2018

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

@swallace21

This comment has been minimized.

Show comment
Hide comment
@swallace21

swallace21 Jul 12, 2018

@andrewkro

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

swallace21 commented Jul 12, 2018

@andrewkro

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

@rajeshkp

This comment has been minimized.

Show comment
Hide comment
@rajeshkp

rajeshkp Jul 24, 2018

Thanks @liedekef it works

rajeshkp commented Jul 24, 2018

Thanks @liedekef it works

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