ubuntu一键安装jdk

ubuntu一键安装jdk

Tags: Ubuntu

摘要

ubuntu一键安装jdk

jdk8一键安装

该脚本主要用于ubuntu14.04
其它系统未验证
wget -O install-jdk8.sh https://coding.net/u/javacore/p/dev-env-shell/git/raw/master/ubuntu-1404-64-install-jdk8.sh && bash install-jdk8.sh

自动安装 jdk-8u112-linux-x64.tar.gz
jdk安装后的目录是 /usr/lib/jvm/jdk1.8.0_112
如果是32位系统则是 /usr/lib/jvm/jdk1.8.0_112
并且设置为默认的jdk

install jdk8

#!/bin/bash
# install jdk-8u112-linux-x64.tar.gz
# apt-get update

if [ ! -d "/tmp/" ]; then
mkdir  "/tmp"
fi

JDK_FILE="/tmp/jdk-8u112-linux-x64.tar.gz"
JDK_NAME="jdk1.8.0_112"
JAVA_dir=/usr/lib/jvm
JDK_PATH=$JAVA_dir/$JDK_NAME

if [ ! -f $JDK_FILE ];then
wget -P "/tmp" "http://files.javablog.net/linux-soft/jdk-8u112-linux-x64.tar.gz"
else
echo "$JDK_FILE is exist";
fi

rm -rf $JAVA_dir
mkdir -p $JAVA_dir
tar zxvf $JDK_FILE -C $JAVA_dir



##写入JAVA_HOME,追加CLASSPATH,PATH
[ -z "`grep ^'export JAVA_HOME=' /etc/profile`" ] && { [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo  "export JAVA_HOME=$JDK_PATH" >> /etc/profile || sed -i "s@^export PATH=@export JAVA_HOME=$JDK_PATH\nexport PATH=@" /etc/profile; } || sed -i "s@^export JAVA_HOME=.*@export JAVA_HOME=$JDK_PATH@" /etc/profile
[ -z "`grep ^'export CLASSPATH=' /etc/profile`" ] && sed -i "s@export JAVA_HOME=\(.*\)@export JAVA_HOME=\1\nexport CLASSPATH=\$JAVA_HOME/lib/tools.jar:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib@" /etc/profile
[ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep '$JAVA_HOME/bin' /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=\$JAVA_HOME/bin:\1@" /etc/profile
[ -z "`grep ^'export PATH=' /etc/profile | grep '$JAVA_HOME/bin'`" ] && echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile


#设置默认
update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/$JDK_NAME/bin/java" 1 
update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/$JDK_NAME/bin/javac" 1 
update-alternatives --set java  /usr/lib/jvm/$JDK_NAME/bin/java 
update-alternatives --set javac /usr/lib/jvm/$JDK_NAME/bin/javac 

echo "$JDK_FILE install success"; 
#rm -f $JDK_FILE

#立即生效
 . /etc/profile

jdk7一键安装

该脚本主要用于ubuntu14.04
其它系统未验证
wget -O ubuntu-1404-32&64-install-jdk7.sh https://coding.net/u/javacore/p/dev-env-shell/git/raw/master/ubuntu-1404-32%252664-install-jdk7.sh && bash ubuntu-1404-32&64-install-jdk7.sh

自动安装open-jdk7
jdk安装后的目录是 /usr/lib/jvm/java-7-openjdk-amd64
如果是32位系统则是 /usr/lib/jvm/java-1.7.0-openjdk-i386
并且设置为默认的jdk

安装jdk7


# install openjdk-7-jdk openjdk-7-jre 
apt-get update
apt-get install -y openjdk-7-jdk openjdk-7-jre


if [ $(getconf WORD_BIT) = '32' ] && [ $(getconf LONG_BIT) = '64' ] ; then
    update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java" 1 
    update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-1.7.¡¤0-openjdk-amd64/bin/javac" 1 
    update-alternatives --set java  /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java 
    update-alternatives --set javac /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/javac  
else
    update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/java-1.7.0-openjdk-i386/bin/java" 1  
    update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-1.7.0-openjdk-i386/bin/javac" 1  
    update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/java-1.7.0-openjdk-i386/bin/javaws" 1  
    update-alternatives --set java   /usr/lib/jvm/java-1.7.0-openjdk-i386/bin/java    
    update-alternatives --set javac    /usr/lib/jvm/java-1.7.0-openjdk-i386/bin/javac  
    update-alternatives --set javaws    /usr/lib/jvm/java-1.7.0-openjdk-i386/bin/javaws   
fi

echo "openjdk-7-jdk install success";