Skip to content

Instantly share code, notes, and snippets.

@terrywang
Last active March 27, 2019 05:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrywang/6361506 to your computer and use it in GitHub Desktop.
Save terrywang/6361506 to your computer and use it in GitHub Desktop.
Oracle JDK Download Script, the magic cookie comes from oab-java6 ;-) It **ONLY** works for latest Oracle JDK 7 and 8 update releases.
#!/bin/bash
# --------------------------------------
#
# Title: Oracle JDK Download Script
# Author: Terry Wang
# Email: i (at) terry (dot) im
# Homepage: http://terry.im
# File: jdk.sh
# Created: 28 August, 2013
#
# Purpose: Get Latest Oracle JDK for Linux
#
# --------------------------------------
########### Setup Variables #############
JAVA_EXT=.tar.gz
# LSB_ARCH=$(dpkg --print-architecture)
ARCH=$(uname -m)
DOWNLOAD_PATH=$PWD
LOG=$PWD/jdk.log
########### Setup Variables #############
# Check command line arguments
if [[ $# -ne 2 ]]; then
echo "Usage: jdk.sh version update"
echo "NOTE: the script works for the latest release updates of Oracle JDK 7 & 8 ONLY!"
echo "NOTE: supported architectures: x86 and x86_64"
echo "Example: to download JDK 8 update 201 (AKA 8u201 or 1.8.0_201) => jdk.sh 8 201"
exit 1
else
JAVA_VER=$1
JAVA_UPD=$2
fi
# Determine machine architecture
if [[ $ARCH == "x86_64" ]]; then
JAVA_BIN="jdk-${JAVA_VER}u${JAVA_UPD}-linux-x64${JAVA_EXT}"
elif [[ $ARCH =~ i[3-6]86 ]]; then
JAVA_BIN="jdk-${JAVA_VER}u${JAVA_UPD}-linux-i586${JAVA_EXT}"
else
echo "Machine architecture $ARCH is NOT supported!"
exit 1
fi
echo "Getting current release download page"
if [[ $JAVA_VER == "8" ]]; then
wget http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html -O /tmp/jdk-download.html >> $LOG 2>&1
else
wget http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html -O /tmp/jdk-download.html >> $LOG 2>&1
fi
DOWNLOAD_FOUND=`grep "jdk-${JAVA_VER}u${JAVA_UPD}-linux-" /tmp/jdk-download.html`
if [[ -z $DOWNLOAD_FOUND ]]; then
echo "Sorry, the magic cookie doesn't work for archive downloads"
echo "Login is required, please manually download using a browser -_-z"
exit 1
fi
# Get the download URL and size
DOWNLOAD_URL=`grep $JAVA_BIN /tmp/jdk-download.html | cut -d'{' -f2 | cut -d',' -f3 | cut -d'"' -f4`
DOWNLOAD_SIZE=`grep $JAVA_BIN /tmp/jdk-download.html | cut -d'{' -f2 | cut -d',' -f2 | cut -d':' -f2 | sed 's/"//g'`
echo "Ready to download JDK ${JAVA_VER}u${JAVA_UPD},size $DOWNLOAD_SIZE"
# Cookie required to download
timestamp=$((`date +%s` + 180000))
COOKIES="oraclelicense=accept-securebackup-cookie;gpw_e24=http://edelivery.oracle.com;s_cc=true;s_sq=%5B%5BB%5D%5D;s_nr=$timestamp"
echo "Downloading JDK ${JAVA_VER}u${JAVA_UPD} for Linux ${ARCH}"
wget --no-check-certificate --header="Cookie: $COOKIES" -c $DOWNLOAD_URL -O $DOWNLOAD_PATH/$JAVA_BIN >> $LOG
ret=$?
if [[ $ret -ne 0 ]]; then
echo "Unfortunately something went wrong..." >&2
exit 1
else
echo "JDK ${JAVA_VER}u${JAVA_UPD} for Linux ${ARCH} download finished, ready to cook!"
fi
exit 0
@gotnix
Copy link

gotnix commented Jul 13, 2015

Cookie 只要 oraclelicense 也可以下载,这样参数就短了不少。例:
wget -c --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie;" 'http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm'

参考 Oracle, I download your JDK by eating magic cookies

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