andforce

switch-jdk-version


很多时候,我们在开发的时候,需要不同的jdk,手动切换比较麻烦,因此我写了个脚本,方便快速切换JDK版本.

#!/bin/bash

JAVA_6_HOME=/usr/lib/jvm/java-6-openjdk-amd64/
JAVA_7_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
JAVA_8_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

echo -e "\n输入编号以选择JDK版本:\n\t1. JDK6($JAVA_6_HOME) \n\t2. JDK7($JAVA_7_HOME) \n\t3. JDK8($JAVA_8_HOME)\n"
echo -n "直接回车会直接选择JDK7:"

read jdk_version
case $jdk_version in
    jdk6|1)
 export JAVA_HOME=$JAVA_6_HOME
 sudo rm /usr/bin/java && sudo ln -s $JAVA_6_HOME/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s $JAVA_6_HOME/bin/javac /usr/bin/javac
 ;;
    jdk7|2)
 export JAVA_HOME=$JAVA_7_HOME
 sudo rm /usr/bin/java && sudo ln -s $JAVA_7_HOME/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s $JAVA_7_HOME/bin/javac /usr/bin/javac
 ;;
    jdk8|3)
 export JAVA_HOME=$JAVA_8_HOME
 sudo rm /usr/bin/java && sudo ln -s $JAVA_8_HOME/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s $JAVA_8_HOME/bin/javac /usr/bin/javac
 ;;
    *)
 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
 sudo rm /usr/bin/java && sudo ln -s /usr/lib/jvm/java-7-openjdk-amd64/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s /usr/lib/jvm/java-7-openjdk-amd64/bin/javac /usr/bin/javac
 ;;
esac

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

java -version