class="shell" name="code">#! /bin/bash # 这个脚本用于在Ubuntu中配置java环境,包括JDK、eclipse和ant。 # # 在使用这个脚本之前,你需要下载JDK、eclipse和ant。目前该脚本只能处理以.tar.gz为后缀的文件。下载时请注意。 # # 下载完成后,将下载到的三个软件和这个脚本放在同一个文件夹下,最好新建一个文件夹。 # # 修改脚本的运行权限(chmod u+x 脚本名),然后在终端中执行该脚本,既可完成Ubuntu下java环境的安装与配置。 # # Aug 16, 2014 # # Author: appreciation # JDK、eclipse和ant的默认安装目录,因个人习惯,我把它们放在/usr/lib/java文件夹下。 # 你也通过指定相应的参数来更改它们的安装目录。 JDK_INSTALL_DIRECTORY=/usr/lib/java ECLIPSE_INSTALL_DIRECTORY=/usr/lib/java ANT_INSTALL_DIRECTORY=/usr/lib/java # print usage function printUsage { echo "Usage: $0 [options]" echo "" echo "Options:" echo "-j | --jdk <jdk_install_dir> Set the directory where the jdk will be installed. Default is \"/usr/lib/java\"" echo "-e | --eclipse <eclipse_install_dir> Set the directory where eclipse will be installed. Default is \"/usr/lib/java\"" echo "-a | --ant <ant_install_dir> Set the directory where ant will be installed. Default is \"/usr/lib/java\"" echo "-h | --help Print this simple help message and exit." } # process parameter function process_param { # 用于存储你将要安装的软件的全名 JDK_NAME=`ls | grep *jdk*.tar.gz` ECLIPSE_NAME=`ls | grep *eclipse*.tar.gz` ANT_NAME=`ls | grep *ant*.tar.gz` while [[ ! -z "$1" ]]; do case "$1" in # 安装jdk的选项 -j | --jdk) JDK_INSTALL_DIRECTORY="$2" shift 2 ;; # 安装eclipse的选项 -e | --eclipse) ECLIPSE_INSTALL_DIRECTORY="$2" shift 2 ;; # 安装ant的选项 -a | --ant) ANT_INSTALL_DIRECTORY="$2" shift 2 ;; -h | --help) printUsage exit 1; ;; *) echo "Unrecognised option: $1" printUsage exit 1; ;; esac done if [ ! -d $JDK_INSTALL_DIRECTORY ]; then sudo mkdir -p $JDK_INSTALL_DIRECTORY fi if [ ! -d $ECLIPSE_INSTALL_DIRECTORY ]; then sudo mkdir -p $ECLIPSE_INSTALL_DIRECTORY fi if [ ! -d $ANT_INSTALL_DIRECTORY ]; then sudo mkdir -p $ANT_INSTALL_DIRECTORY fi } # 调用tar命令来安装这三个软件 function installJava { sudo xterm -T JDK -e "tar -zvxf $JDK_NAME -C $JDK_INSTALL_DIRECTORY" & sudo xterm -T eclipse -e "tar -zvxf $ECLIPSE_NAME -C $ECLIPSE_INSTALL_DIRECTORY" & sudo xterm -T apache -e "tar -zvxf $ANT_NAME -C $ANT_INSTALL_DIRECTORY" & sleep 5 } # 安装完成后,设置相应的环境变量。这些变量将会写进~/.bashrc文件的结尾。 function configJava { JDK_NAME=`ls $JDK_INSTALL_DIRECTORY | grep jdk` ECLIPSE_NAME=`ls $ECLIPSE_INSTALL_DIRECTORY | grep eclipse` ANT_NAME=`ls $ANT_INSTALL_DIRECTORY | grep ant` # 由于访问权限的问题,eclipse下的jre链接可能会损坏。因此我强制删除(ln的-f选项)原本的jre链接,然后创建 # 一个新的jre链接。由于加了sudo,新的jre链接将不会再有访问权限的问题。 sudo ln -sf $JDK_INSTALL_DIRECTORY/$JDK_NAME/jre $ECLIPSE_INSTALL_DIRECTORY/$ECLIPSE_NAME/jre sudo echo "export JAVA_HOME=$JDK_INSTALL_DIRECTORY/$JDK_NAME" >> ~/.bashrc sudo echo "export JRE_HOME=\${JAVA_HOME}/jre" >> ~/.bashrc sudo echo "export ANT_HOME=$ANT_INSTALL_DIRECTORY/$ANT_NAME" >> ~/.bashrc sudo echo "export CLASSPATH=\${JAVA_HOME}/lib:\${JRE_HOME}/lib:\${ANT_HOME}/lib:" >> ~/.bashrc sudo echo "export PATH=\${JAVA_HOME}/bin:\${JRE_HOME}/bin:\${ANT_HOME}/bin:\$PATH" >> ~/.bashrc } # 在桌面上创建eclipse图标 function makeIcon { rm -f ~/Desktop/eclipse.desktop touch ~/Desktop/eclipse.desktop echo "[Desktop Entry]" >> ~/Desktop/eclipse.desktop echo "Categories=Development;" >> ~/Desktop/eclipse.desktop echo "Comment[zh_CN]=" >> ~/Desktop/eclipse.desktop echo "Exec=$ECLIPSE_INSTALL_DIRECTORY/$ECLIPSE_NAME/eclipse" >> ~/Desktop/eclipse.desktop echo "GenericName[zh_CN]=IDE" >> ~/Desktop/eclipse.desktop echo "GenericName=IDE" >> ~/Desktop/eclipse.desktop echo "Icon=$ECLIPSE_INSTALL_DIRECTORY/$ECLIPSE_NAME/icon.xpm" >> ~/Desktop/eclipse.desktop echo "MimeType=" >> ~/Desktop/eclipse.desktop echo "Name[zh_CN]=eclipse" >> ~/Desktop/eclipse.desktop echo "Name=eclipse" >> ~/Desktop/eclipse.desktop echo "Path=" >> ~/Desktop/eclipse.desktop echo "StartupNotify=true" >> ~/Desktop/eclipse.desktop echo "Terminal=false" >> ~/Desktop/eclipse.desktop echo "Type=Application" >> ~/Desktop/eclipse.desktop echo "X-DBUS-ServiceName=" >> ~/Desktop/eclipse.desktop echo "X-DBUS-StartupType=" >> ~/Desktop/eclipse.desktop echo "X-KDE-SubstituteUID=false" >> ~/Desktop/eclipse.desktop echo "X-KDE-Username=owen" >> ~/Desktop/eclipse.desktop chmod u+x ~/Desktop/eclipse.desktop } # 脚本主体部分,依次调用上面定义的四个函数,完成安装和配置过程 process_param $* installJava configJava makeIcon
?