linux一键安装java脚本

本文介绍的脚本一键安装java,我们有时候自己安装java,不是不会安装,就是有时候容易忘记命令,忘记jdk的安装包在哪里,这时候,您可以记住本站,一键安装脚本会为你节省很多很多时间。

时间是用在女朋友身上的是本站一向对程序员朋友的忠告


一键安装java脚本

wget http://66-ai.com/download/script-litte-prince/app/install-java.sh -O /root/install-java.sh && sh install-java.sh

上面的代码是自动安装,如果想看看install-java.sh 脚本中是什么,可以看下面的脚本:

#!/bin/bash
set -e

java_dist="/root/jdk-8u261-linux-x64.tar.gz"
default_java_dir="/usr/lib/jvm"
java_dir="$default_java_dir"
SUDO_USER=root

function usage() {
    echo ""
    echo "这个脚本会自动下载jdk 1.8. "
    echo "Usage: "
    echo "install-java.sh -f <java_dist> [-p <java_dir>]"
    echo ""
    echo "-f: The jdk tar.gz file. 默认安装的是/root/jdk-8u261-linux-x64.tar.gz"
    echo "-p: java默认安装在/usr/lib/jvm目录中,你可以通过-p命令切换目录"
    echo "-h: 显示帮助."
    echo ""
}

function confirm() {
    # call with a prompt string or use a default
    read -r -p "${1:-Are you sure?} [y/N] " response
    case $response in
    [yY][eE][sS] | [yY])
        true
        ;;
    *)
        false
        ;;
    esac
}

# Make sure the script is running as root.
if [ "$UID" -ne "0" ]; then
    echo "You must be root to run $0. Try following"
    echo "sudo $0"
    exit 9
fi

while getopts "f:p:h" opts; do
    case $opts in
    f)
        java_dist=${OPTARG}
        ;;
    p)
        java_dir=${OPTARG}
        ;;
    h)
        usage
        exit 0
        ;;
    \?)
        usage
        exit 1
        ;;
    esac
done

if ! [ -x "$(command -v axel)" ]; then
  wget http://66-ai.com/download/script-litte-prince/jdk-8u261-linux-x64.tar.gz -O /root/jdk-8u261-linux-x64.tar.gz
else
  axel -n 10 -a http://66-ai.com/download/script-litte-prince/jdk-8u261-linux-x64.tar.gz -o /root/jdk-8u261-linux-x64.tar.gz
fi

if [[ ! -f $java_dist ]]; then
    echo "Please specify the Java distribution file."
    echo "Use -h for help."
    exit 1
fi

# Validate Java Distribution
java_dist_filename=$(basename $java_dist)

if [[ ${java_dist_filename: -7} != ".tar.gz" ]]; then
    echo "Java distribution must be a valid tar.gz file."
    exit 1
fi

# Create the default directory if user has not specified any other path
if [[ $java_dir == $default_java_dir ]]; then
    mkdir -p $java_dir
fi

#Validate java directory
if [[ ! -d $java_dir ]]; then
    echo "Please specify a valid Java installation directory."
    exit 1
fi

echo "Installing: $java_dist_filename"

# Check Java executable
java_exec="$(tar -tzf $java_dist | grep ^[^/]*/bin/java$ || echo "")"

if [[ -z $java_exec ]]; then
    echo "Could not find \"java\" executable in the distribution. Please specify a valid Java distribution."
    exit 1
fi

# JDK Directory with version
jdk_dir="$(echo $java_exec | cut -f1 -d"/")"
extracted_dirname=$java_dir"/"$jdk_dir

# Extract Java Distribution
if [[ ! -d $extracted_dirname ]]; then
    echo "Extracting $java_dist to $java_dir"
    tar -xof $java_dist -C $java_dir
    echo "JDK is extracted to $extracted_dirname"
else
    echo "WARN: JDK was not extracted to $java_dir. There is an existing directory with the name \"$jdk_dir\"."
    if ! (confirm "Do you want to continue?"); then
        exit 1
    fi
fi

if [[ ! -f "${extracted_dirname}/bin/java" ]]; then
    echo "ERROR: The path $extracted_dirname is not a valid Java installation."
    exit 1
fi

# Oracle JDK: 7 to 8
java_78_dir_regex="^jdk1\.([0-9]*).*$"
# Oracle JDK / OpenJDK / AdoptOpenJDK: 9 and upwards
java_9up_dir_regex="^jdk-([0-9]*).*$"

# JDK Major Version
jdk_major_version=""

if [[ $jdk_dir =~ $java_78_dir_regex ]]; then
    jdk_major_version=$(echo $jdk_dir | sed -nE "s/$java_78_dir_regex/\1/p")
else
    jdk_major_version=$(echo $jdk_dir | sed -nE "s/$java_9up_dir_regex/\1/p")
fi

# Install Demos

if [[ $jdk_dir =~ $java_78_dir_regex ]]; then
    # Demos are only available for Java 7 and 8
    demos_dist=$(dirname $java_dist)"/"$(echo $java_dist_filename | sed 's/\.tar\.gz/-demos\0/')
fi

if [[ -f $demos_dist && ! -d $extracted_dirname/demo ]]; then
    # No demo directory
    if (confirm "Extract demos?"); then
        echo "Extracting $demos_dist to $java_dir"
        tar -xf $demos_dist -C $java_dir
    fi
fi

# Install Unlimited JCE Policy (only for Oracle JDK 7 & 8)
# Java 9 and above: default JCE policy files already allow for \"unlimited\" cryptographic strengths.

unlimited_jce_policy_dist=""

if [[ $jdk_dir =~ ^jdk1\.7.* ]]; then
    unlimited_jce_policy_dist="$(dirname $java_dist)/UnlimitedJCEPolicyJDK7.zip"
elif [[ $jdk_dir =~ ^jdk1\.8.* ]]; then
    unlimited_jce_policy_dist="$(dirname $java_dist)/jce_policy-8.zip"
fi

if [[ -f $unlimited_jce_policy_dist ]]; then
    #Check whether unzip command exsits
    if ! command -v unzip >/dev/null 2>&1; then
        echo "Please install unzip (apt -y install unzip)."
        exit 1
    fi
    if (confirm "Install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files?"); then
        echo "Extracting policy jars in $unlimited_jce_policy_dist to $extracted_dirname/jre/lib/security"
        unzip -j -o $unlimited_jce_policy_dist *.jar -d $extracted_dirname/jre/lib/security
    fi
fi

# Run update-alternatives commands
if (confirm "Run update-alternatives commands?"); then
    echo "Running update-alternatives..."
    cmd="update-alternatives --install /usr/bin/java java $extracted_dirname/bin/java 10000"
    declare -a commands=($(ls -1 ${extracted_dirname}/bin | grep -v ^java$))
    for command in "${commands[@]}"; do
        command_path=$extracted_dirname/bin/$command
        if [[ -x $command_path ]]; then
            cmd="$cmd --slave /usr/bin/$command $command $command_path"
        fi
    done
    lib_path=$extracted_dirname/jre/lib/amd64/libnpjp2.so
    if [[ -d "/usr/lib/mozilla/plugins/" ]] && [[ -f $lib_path ]]; then
        cmd="$cmd --slave /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so $lib_path"
    fi
    echo $cmd
    # Execute command
    $cmd
    update-alternatives --set java $extracted_dirname/bin/java
fi

# Create system preferences directory
java_system_prefs_dir="/etc/.java/.systemPrefs"
if [[ ! -d $java_system_prefs_dir ]]; then
    if (confirm "Create Java System Prefs Directory ($java_system_prefs_dir) and change ownership to $SUDO_USER:$SUDO_USER?"); then
        echo "Creating $java_system_prefs_dir"
        mkdir -p $java_system_prefs_dir
        chown -R $SUDO_USER:$SUDO_USER $java_system_prefs_dir
    fi
fi

USER_HOME="$(getent passwd $SUDO_USER | cut -d: -f6)"

if [[ -d "$USER_HOME" ]] && (confirm "Do you want to set JAVA_HOME environment variable in $USER_HOME/.bashrc?"); then
    if grep -q "export JAVA_HOME=.*" $USER_HOME/.bashrc; then
        sed -i "s|export JAVA_HOME=.*|export JAVA_HOME=$extracted_dirname|" $USER_HOME/.bashrc
    else
        echo "export JAVA_HOME=$extracted_dirname" >>$USER_HOME/.bashrc
    fi
fi

applications_dir="$USER_HOME/.local/share/applications"

create_jmc_shortcut() {
    shortcut_file="$applications_dir/jmc_$jdk_major_version.desktop"
    cat <<_EOF_ >$shortcut_file
[Desktop Entry]
Name=Java $jdk_major_version: JMC
Comment=Oracle Java Mission Control for Java $jdk_major_version
Type=Application
Exec=$extracted_dirname/bin/jmc
Icon=$extracted_dirname/lib/missioncontrol/icon.xpm
Terminal=false
_EOF_
    chmod +x $shortcut_file
}

if [[ -d $applications_dir ]] && [[ -f $extracted_dirname/bin/jmc ]]; then
    if (confirm "Do you want to create a desktop shortcut to JMC?"); then
        create_jmc_shortcut
    fi
fi