如何通过脚本下载并安装 Oracle Java?

如何通过脚本下载并安装 Oracle Java?

热门WebUpd8 团队 PPA尚未更新。

它仍然有像 8u45 这样的过时版本。

我从 Oracle 站点手动下载了 JDK 并替换了它。

有没有办法自动检查 Oracle java 最新版本并下载(如果已更新)?

我发现主要问题是下载。我查看了oracle-java8-installer脚本,发现他们下载了一些奇怪的链接。

如果不接受许可证,则无法直接从 Oracle 站点下载 tarball。安装程序也检查了这一点。但我不太明白如何从命令行接受许可证。

该脚本可以添加到 cron。

以防 PPA 从未更新,或者出现像这次一样的延迟。

答案1

该脚本与WebUpd8 PPA. 因此首先通过 PPA 安装 Java。

如果 PPA 中有新版本,则通过包管理器安装。与此同时,如果 Oracle 网站上有新版本,脚本可以安装较新的版本。


一些注意事项

通过 下载版本wget,例如:

wget --no-cookies --header \
    "Cookie: oraclelicense=accept-securebackup-cookie" \
    $URL

linux-x64使用 (for和tar.gz)获取 URL

URL=$(curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
    awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /linux-x64/ && /\.tar\.gz/" | \
    grep -o 'http.*\.tar\.gz')

一体

为了Oracle Java 8通过 URLhttp://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
    $(curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
        awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /linux-x64/ && /\.tar\.gz/" | \
        grep -o 'http.*\.tar\.gz'\
    )

解释

  • /downloads\['/– 我们需要该downloads区域

  • /\['files'\]/– 我们需要该files区域

  • /linux-x64/– 目标架构

    你可以适应。

  • /\.tar\.gz/– 文件格式

    你可以适应。

    如果你这样做,不要tar.gz忘记grep -o 'http.*\.tar\.gz'

  • ! /demos/– 没有演示


Oracle Java 8、x64、tar.gz 的脚本

#!/bin/bash
major_version="8"
target="/usr/lib/jvm/java-8-oracle"
arch="linux-x64"

# This URL is for the major version 8.
download_page="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html"

download_url=$(curl -s "$download_page" | \
        awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /$arch/ && /\.tar\.gz/" | \
        grep -o 'http.*\.tar\.gz')

server=$(awk -Fjdk-"$major_version"u '{split($2,a,/-/); print a[1]}' <<< "$download_url")
local=$("$target"/bin/java -version 2>&1 | awk -F_ '/java version/ {gsub(/"/,""); print $2}')

[ "$local" == "" ] && local=0

filename=$(awk -F'\/' '{print $NF}' <<< "$download_url")

[ "$server" -gt "$local" ] && wget -q -P "/tmp" --no-cookies --show-progress --header \
    "Cookie: oraclelicense=accept-securebackup-cookie" \
    "$download_url" 

if [ -f "$filename" ]; then
    mkdir -p "$target"
    tar -xf "$filename" --strip 1 -C "$target"
fi

exit 0

有用

使用此命令列出所有体系结构和文件格式:

curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
        awk "/downloads\['/ && ! /demos/ && /\['files'\]\['jdk/"

示例输出

downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-i586.rpm'] = { "title":"Linux x86", "size":"146.9 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-i586.rpm"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-i586.tar.gz'] = { "title":"Linux x86", "size":"166.95 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-i586.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-x64.rpm'] = { "title":"Linux x64", "size":"145.19 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.rpm"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-linux-x64.tar.gz'] = { "title":"Linux x64", "size":"165.25 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-macosx-x64.dmg'] = { "title":"Mac OS X x64", "size":"222.09 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-macosx-x64.dmg"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-sparcv9.tar.Z'] = { "title":"Solaris SPARC 64-bit (SVR4 package)", "size":"139.36 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-sparcv9.tar.Z"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-sparcv9.tar.gz'] = { "title":"Solaris SPARC 64-bit", "size":"98.8 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-sparcv9.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-x64.tar.Z'] = { "title":"Solaris x64 (SVR4 package)", "size":"139.79 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-x64.tar.Z"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-solaris-x64.tar.gz'] = { "title":"Solaris x64", "size":"96.45 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-solaris-x64.tar.gz"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-windows-i586.exe'] = { "title":"Windows x86", "size":"176.02 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-windows-i586.exe"};
downloads['jdk-8u51-oth-JPR']['files']['jdk-8u51-windows-x64.exe'] = { "title":"Windows x64", "size":"180.51 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-windows-x64.exe"};

答案2

这将自动下载当前运行的内核架构的最新 Oracle Java 版本,但目前缺少安装部分(注意:在示例中,我根据自定义文本文件检查了当前 Java 版本,以欺骗实际的 Java 版本

JavaUpdater.sh

#!/bin/bash
echo 'Checking for a new Java Version...'
kern_arch=$(uname -r | sed 's/.*-\(.*\)/\1/')
if [ "$kern_arch" = amd64 ]; then
    java_ver=' x64'
fi
current_version=$(java -version 2>&1 >/dev/null | sed -n '1s/.*"\([^"]*\)"/\1/p')
latest_version_url=$(curl https://www.java.com/en/download/manual.jsp 2>/dev/null | grep -Po -m 1 "href=\"\K[^\"]*(?=.*Linux${java_ver} en JRE)"
)
latest_version=$(curl $latest_version_url 2>/dev/null | sed -n 's/.*File=jre-\([0-9]\)u\([0-9]\{2\}\).*/1.\1.0_\2/p')
if [ "$current_version" = "$latest_version" ]; then
    echo 'No new Java version available. Aborting.'
    exit 1
fi
read -n 1 -p "A new Java version is available (${latest_version})! Would you like to download it (y)? " download
echo
if [ "$download" = y ]; then
    filename="$(curl "$latest_version_url" 2> /dev/null | sed -n 's/.*File=\([^&]*\).*/\1/p')"
    wget -q -O "$filename" --show-progress "$latest_version_url"
    echo 'Download completed.'
    exit 0
fi
exit 1
  • 检查当前运行的内核的体系结构,以选择检查更新并最终下载哪个 Java 版本
  • 检查当前 Java 版本是否与可从以下网址下载的最新 Java 版本一致https://www.java.com/en/download/manual.jsp
  • 让用户决定是否下载新版本

注意事项:

  • 到目前为止,它假设只安装了一个 Oracle Java
  • 该脚本依赖于页面的当前结构https://www.java.com/en/download/manual.jsp;Oracle 方面对页面结构的更新可能会破坏它;
  • 该脚本依赖于可下载的最新版本的文件名来确定最新版本;例如,可下载的最新 64 位版本名为 ,jre-8u51-linux-x64.tar.gz将被解释为 Java 8 Update 51(就 Java 版本而言,1.8.0_51);
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
└── JavaUpdater.sh

0 directories, 2 files
user@debian ~/tmp/JavaDownloader
 % cat current_version.txt 
java version "1.8.0_50"
Java(TM) SE Runtime Environment (build 1.8.0_50-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.50-b03, mixed mode)
user@debian ~/tmp/JavaDownloader
 % bash JavaUpdater.sh 
Checking for a new Java Version...
A new Java version is available (1.8.0_51)! Would you like to download it (y)? y
jre-8u51-linux-x64. 100%[=====================>]  60.90M  1.21MB/s   in 51s    
Download completed.
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
├── JavaUpdater.sh
└── jre-8u51-linux-x64.tar.gz

0 directories, 3 files
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
├── JavaUpdater.sh
└── jre-8u51-linux-x64.tar.gz

0 directories, 3 files
user@debian ~/tmp/JavaDownloader
 % cat current_version.txt 
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
user@debian ~/tmp/JavaDownloader
 % bash JavaUpdater.sh 
Checking for a new Java Version...
No new Java version available. Aborting.
user@debian ~/tmp/JavaDownloader
 % tree
.
├── current_version.txt
├── JavaUpdater.sh
└── jre-8u51-linux-x64.tar.gz

0 directories, 3 files

答案3

这是基于@AB 回答的脚本。

此脚本安装 Oracle java 8 64 位。每次运行时,它都会检查版本并将 java 更新为最新版本。

可以将其放置到cron.dailycron.weekly来检查更新。

#!/bin/sh

J_INSTALL_DIR=/usr/lib/jvm/java-8-oracle

URL=$(curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | \
    awk "/downloads\['/ && ! /demos/ && /\['files'\]/ && /linux-x64/ && /\.tar\.gz/" | \
    grep -o 'http.*\.tar\.gz') 

AVAILABLE=$(echo $URL | grep -o -P 'jdk-8u.{0,2}' | cut -d "u" -f 2)
CURRENT=$("$J_INSTALL_DIR"/bin/java -version 2>&1 | awk '/version/ {print $3}' | cut -d_ -f 2 | tr -d '"')

if [ -z $CURRENT ] || [ $AVAILABLE -gt $CURRENT ]; then
    cd /var/cache/oracle-jdk8-installer
    rm -f jdk-8u"$CURRENT"-linux-x64.tar.gz
    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" $URL
    tar -xzf jdk-8u"$AVAILABLE"-linux-x64.tar.gz
    rm -rf $J_INSTALL_DIR
    mv jdk1.8.0_"$AVAILABLE"/ $J_INSTALL_DIR

    LATEST=$(LANG=C update-alternatives --display java | grep ^/ | sed -e 's/.* //g' | sort -n | tail -1)
    if [ -z $LATEST ]; then
        LATEST=1
    else
        J_PATH=$(LANG=C update-alternatives --display java | grep "priority "$LATEST"" | awk '{print $1}')
        [ $J_PATH = "$J_INSTALL_DIR"/jre/bin/java ] || LATEST=$((LATEST+1))
    fi  

    #link JRE files
    for f in $J_INSTALL_DIR/jre/bin/*; do
        name=`basename $f`;
        if [ ! -f "/usr/bin/$name" -o -L "/usr/bin/$name" ]; then  #some files, like jvisualvm might not be links
            if [ -f "$J_INSTALL_DIR/man/man1/$name.1.gz" ]; then

                update-alternatives --install /usr/bin/$name $name $J_INSTALL_DIR/jre/bin/$name $LATEST --slave /usr/share/man/man1/$name.1.gz $name.1.gz $J_INSTALL_DIR/man/man1/$name.1.gz
            fi
        else     
                update-alternatives --install /usr/bin/$name $name $J_INSTALL_DIR/jre/bin/$name $LATEST

        fi
    done

    echo "Oracle Java 8 installed"

fi

if [ $AVAILABLE -eq $CURRENT ]; then
    echo "Java is up to date"
fi

为了立即测试它,您可以CURRENT=45在脚本中进行设置。

但可以做一些小的改进,比如 fontconfig。

该脚本可以从以下网址下载github也。

欢迎提出请求和投诉。

相关内容