JDK download from command line

Since Oracle started to prevent redistribution of the JDK,  for some of us, connected to our systems only with a shell and without a debian based distribution, (see: How To Install Oracle Java 7 In Debian Via Repository) that was really annoying.

So I’ve made my script that’s a little bit different from what I’ve found on google and stackoverflow.

For the impatients, you use it in this way:
# jdk-download 8u40 i586

Here’s the code:

#!/bin/bash

if [ "$#" -lt 2 ]; then
    echo -e "Download the oracle JDK from command line\n"
    echo -e " $0 <version> <platform> [<build>]\n"
    echo     "  <version>   Something like 8u40"
    echo     "  <platform>  Usually i586 or x64"
    echo     "  <build>     The internal build number used by oracle, to avoid guessing and"
    echo -e "              trying to download starting from 99 to 1 (build 0, really?!!)\n"
    exit 1
fi

#Something like "8u40"
VER=$1
#Expected "i586" or "x64"
PLATFORM=$2

HEADER="Cookie: oraclelicense=accept-securebackup-cookie"

function download() {
	BUILD=$1
	BASE_URL="http://download.oracle.com/otn-pub/java/jdk/${VER}-b${BUILD}/"
	FILENAME="jdk-${VER}-linux-${PLATFORM}.tar.gz"
	echo "Try download build number ${BUILD}: ${BASE_URL}${FILENAME}"
	wget -q -c --show-progress -O "${FILENAME}" --no-check-certificate --no-cookies --header "${HEADER}" "${BASE_URL}${FILENAME}"  && {\
		echo "Download completed"
		exit 0
	}
}


[ "$#" -eq 3 ] && {\
	download $3
	echo "File not found"
	exit 1
}

for num in {99..10} 0{9..1}
do
	download $num
done

echo "File not found... sorry"

Trackbacks & Pings

Leave a Reply

Your email address will not be published. Required fields are marked *