是否有任何 API 可以获取最新的 JAVA 8。对于我的自动构建系统,我需要获取最新版本。
类似于:
wget https://oracle.com/api/download/jre/8/Linux x64/lastest
这将为我提供最新版本(在本例中构建版本为 u131,截至 05.05.17)
或者我需要去 http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html,自己做acceptLicense-JS,解析html并获取链接吗?
答案1
对于我的自动构建系统,我需要获取最新版本。
您可以使用weget
或curl
来执行此操作。
解决方案 1 使用wget
使用wget
并使用特殊的 cookie 来绕过许可协议:
只需一个 cookie 即可绕过此问题(您仍然必须同意安装条款)
该 Cookie 是:
Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie
例子
如果你想使用 下载适用于 64 位 Linux(例如 Ubuntu)的 jdk7u4
wget
,你可以使用:wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u4-b20/jdk-7u4-linux-x64.tar.gz"
来源使用脚本下载 Oracle Java JRE 和 JDK
解决方案 1 使用curl
有一个 shell 脚本可以完全自动化下载。
下载JavaAutomatic.ksh
#!/bin/ksh
###################################
## Overview
###################################
# Downloads JDK & JRE from download.oracle.com by first walking possible combinations of available version/build numbers.
# Once a version/build is found, subsequent attempts in the same family are primed to avoid walking the entire tree again.
#
# version=<x> represents the highest attempted version number while walking the tree
# build=<y> represents the highest attempted build number while walking the tree
#
# Adjust "BASE" to control which versions are downloaded. A value of 8 corresponds to Java-v1.8.x
# Likewise a value of "6 7 8" equates to Java-v1.6.x, Java-v1.7.x, Java-v1.8.x
#
# Adjust "PLATFORMS" to match the architecture platforms you wish to download.
#
# Reference URLS:
# https://gist.github.com/P7h/9741922
# http://download.oracle.com/otn-pub/java/jdk/8u40-b26/jre-8u40-windows-x64.exe
# http://download.oracle.com/otn-pub/java/jdk/8u40-b27/jdk-8u40-macosx-x64.dmg
###################################
## Usage
###################################
# downloadJavaAutomatic.ksh
# Finds and downloads current Java versions
# downloadJavaAutomatic.ksh [-f <file>]
# Skips version/build find routing, uses external <file>
# which contains a list of URLs to be downloaded.
###################################
## Config
###################################
version=99
build=30
set -A BASE 8 7 6
set -A BASE 7
set -A PLATFORMS -- -linux-x64.tar.gz -linux-i586.tar.gz -macosx-x64.dmg -windows-i586.exe -windows-x64.exe
###################################
## Subroutines
###################################
doCountCurl() {
ps -ef | egrep -ie "curl.*${cookie}" | grep -vi grep | wc -l
}
doGetURL() {
printf "\n$URL\n"
Cookie="Cookie:oraclelicense=accept-securebackup-cookie"
curl -O -L -S -H "${Cookie}" --progress-bar --connect-timeout 55 --fail -k "${1}"
if [[ $? -ne 0 ]]; then
echo ERROR - download failed.
fi
}
doTestURL() {
uri_local=$1
base_local=$2
version_local=$3
build_local=$4
platform_local=$5
type_local=$6
Cookie="Cookie:oraclelicense=accept-securebackup-cookie"
URL="http://download.oracle.com/${uri_local}${base_local}u${version_local}-b${build_local}/${type_local}-${base_local}u${version_local}${platform_local}"
curl --output /dev/null -L -sS -H "${Cookie}" --head --connect-timeout 15 --max-time 30 --fail -k "${URL}" 2>/dev/null
if [[ $? -eq 0 ]]; then
echo $URL >> $urlFile
printf "$version_local" > $tmpVersion
printf "$build_local" > $tmpBuild
printf "${URL}" > $tmpFile
break
fi
}
doFindVersionBuild() {
URI=$1
BASE=$2
PLATFORM=$3
TYPE=$4
### If available use version to limit tree walking
if [[ -e $tmpVersion ]]; then
version_cnt=$(( `cat $tmpVersion` ))
else
version_cnt=$version
fi
while [[ $version_cnt -ge 1 ]] && [[ ! -e $tmpFile ]]; do
### If available use build to limit tree walking
if [[ -e $tmpBuild ]]; then
build_cnt=$(( 10 + `cat $tmpBuild` ))
else
build_cnt=$build
fi
### Keep going until a version is found
while [[ $build_cnt -ge 1 ]] && [[ ! -e $tmpFile ]]; do
### Limit HTTP connections to avoid saturating connection
maxHTTP=50; while [[ `doCountCurl` -ge ${maxHTTP} ]]; do sleep 1; done
### Send to background to speed up process
doTestURL ${URI} ${BASE} ${version_cnt} ${build_cnt} ${PLATFORM} ${TYPE} &
(( build_cnt-=1 ))
done
(( version_cnt -= 1 ));
sleep 1
done
### Wait for current version/build series to finish before starting next round
while [[ `doCountCurl` -gt 0 ]]; do sleep 1; done
### Save results to VERSION array. Highest version will be element #1
set -A VERSION dummy `[[ -e $tmpFile ]] && sort -nr $tmpFile`
if [[ "${VERSION[1]}" != "" ]]; then
echo ${VERSION[1]}
else
printf -- "${TYPE}-${BASE}u??${PLATFORM} - No Version found.\n"
fi
### Cleanup
if [[ -e $tmpFile ]]; then
rm $tmpFile
else
[[ -e $tmpVersion ]] && rm $tmpVersion
[[ -e $tmpBuild ]] && rm $tmpBuild
fi
}
doDownload() {
printf "\nDownloading...\n"
for URL in $( grep -vi "#" $urlFile ); do
doGetURL "$URL"
done
}
#########################
### Main
#########################
tmpFile=$$-tmp.txt
tmpVersion=$$-version.txt
tmpBuild=$$-build.txt
urlFile=url.txt
clear
if [[ $1 = "-f" ]] && [[ -r "$2" ]]; then
echo "Search for version/build bypassed. Using URLs within file: $2"
urlFile=$2
doDownload
else
printf "Searching for available versions...\n\n"
for base in ${BASE[@]}; do
for type in $(echo jre jdk); do
printf "# `date`\n" >> $urlFile
for platform in ${PLATFORMS[@]}; do
URI=otn-pub/java/jdk/
doFindVersionBuild ${URI} ${base} ${platform} ${type}
done
done
### Delete version and build markers before attempting next BASE version.
[[ -e $tmpVersion ]] && rm $tmpVersion
[[ -e $tmpBuild ]] && rm $tmpBuild
done
doDownload
fi
[[ -e $urlFile ]] && rm $urlFile