From ad93dc3683934e39f5fbdc69088fdc5c353868d8 Mon Sep 17 00:00:00 2001 From: Md Jahidul Hamid Date: Wed, 24 Aug 2016 05:52:16 +0600 Subject: [PATCH] update --- README.md | 82 +++++++++++++----- oraji | 244 +++++++++++++++++++++++++++++++++++++++--------------- oraji.1 | 8 +- 3 files changed, 242 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index ae9b3e5..25de709 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ Oracle Java Installer ==================== -Oracle java (jdk/jre) installler script for linux. It simplifies the installation of Oracle java from a [download](http://www.oracle.com/technetwork/java/javase/downloads/index.html)ed source (tar.gz). It also maintains consistency among multiple versions of java (jdk/jre) installed with this script. Uninstalling any java version installed with this script will fall back to the next most recently installed (with this script) version of java. +Oracle java (jdk/jre) installler script for linux. It simplifies the installation of Oracle java from a [download](http://www.oracle.com/technetwork/java/javase/downloads/index.html)ed source (tar.gz). It also maintains consistency among multiple versions of java (jdk/jre). Uninstalling any java version installed with this script will fall back to the next most recently installed version of java. Install oraji: ===================== -Give the install.sh file execution permission and run it in terminal (with root privillege). +Give the install.sh file execution permission and run it in terminal (with root privilege). ```sh sudo ./install.sh @@ -36,9 +36,8 @@ Java Uninstallation Instruction: ============================ 1. Run `oraji -u` (or `path/to/oraji -u` if you didn't install the *oraji* script). -2. It will ask for java version. An auto detected version is provided as suggestion. If that is correct, just hit Enter without making any changes. -3. Otherwise if the java version provided by default isn't correct (this shouldn't happen), provide manually and then hit Enter. -4. The version can be provided as command line argument too i.e `oraji -u version` (or `/path/to/oraji -u version` if you didn't install the script). +2. Choose the version to be uninstalled from the list. +3. The version can be provided as command line argument too i.e `oraji -u version` (or `/path/to/oraji -u version` if you didn't install the script). **Note:** @@ -48,20 +47,58 @@ Java Uninstallation Instruction: **N.B:** The uninstallation method is only for the oracle java installed with the `oraji` script. It deletes the `/usr/lib/jvm/jdk|jre` directory and all exports and environment variables that was included in */etc/profile* by oraji and the mozilla plugin. If a fall back java is available, exports and environment variables will be available for this java version and mozilla plugin will be restored. + +Manage multiple Java versions: +=========================== +You can install multiple Java versions and set whichever version you want as your current Java. + +To check which version of Java is currently active (with all other available Java versions) run: + +```sh +oraji -c +``` + +To activate a Java version, run: + +```sh +sudo oraji -s +``` +and select the version from the list. + + +Options: +======= + +Option | Alt. Option | Details +---- | ------ | ----- +`-v` | `--version` | Show version +`-h` | `--help` | Show help +`-i` | `--install` | Install from a archive path +`-u` | `--uninstall` | Uninstall java +`-s` | `--set` | Set a java version +`-c` | `--check` | Show the installed java versions + + #ChangeLog: -###`3.0.0`: Tue Dec 22 19:32:07 UTC 2015 +###`4.1.0`: Tue Aug 23 23:38:01 UTC 2016 -1. Improved portability. -2. Lot's of bug fixed. -3. Comprehensive install of java which makes it usable for java developers. +1. `-s` option to activate an installed Java version +2. `-c` option to check installed Java version +3. `-h` option for help +4. `-i` option for an alternative to provide archive path -###`3.0.1`: Tue Dec 22 20:58:42 UTC 2015 +###`4.0.2`: Sat Jan 9 16:16:23 UTC 2016 -1. Added some extra environment variables besides JAVA_HOME. -2. Updated PATH environment variables with some new paths. -3. JAVA_HOME is no longer deleted by uninstall script. -4. Multiple java version management. +1. Improved environment variable management for multiple java version. +2. Minor bug fix. +3. Manual added. Can be accessed with `man oraji`. + +###`4.0.1`: Wed Dec 23 04:34:25 UTC 2015 + +1. Input prompt improved. +2. tilde (~) expansion supported in input prompt. +3. Minor bug fix ###`4.0.0`: Tue Dec 22 23:37:16 UTC 2015 @@ -74,17 +111,18 @@ Java Uninstallation Instruction: 6. `oraji -u` to uninstall java. 7. Internal multiple java version management. -###`4.0.1`: Wed Dec 23 04:34:25 UTC 2015 +###`3.0.1`: Tue Dec 22 20:58:42 UTC 2015 -1. Input prompt improved. -2. tilde (~) expansion supported in input prompt. -3. Minor bug fix +1. Added some extra environment variables besides JAVA_HOME. +2. Updated PATH environment variables with some new paths. +3. JAVA_HOME is no longer deleted by uninstall script. +4. Multiple java version management. -###`4.0.2`: Sat Jan 9 16:16:23 UTC 2016 +###`3.0.0`: Tue Dec 22 19:32:07 UTC 2015 -1. Improved environment variable management for multiple java version. -2. Minor bug fix. -3. Manual added. Can be accessed with `man oraji`. +1. Improved portability. +2. Lot's of bug fixed. +3. Comprehensive install of java which makes it usable for java developers. Tested OS: ============= diff --git a/oraji b/oraji index f6bc04e..3042be2 100644 --- a/oraji +++ b/oraji @@ -1,6 +1,6 @@ #!/bin/bash project="oraji" -version="4.0.2" +version="4.1.0" author="Md. Jahidul Hamid" source="https://github.com/neurobin/$project" bug_report="$source/issues" @@ -11,20 +11,41 @@ Author : $author Source : $source Bug report : $bug_report " - -[[ $1 == "-v" || $1 == "--version" ]] && { echo "$version_info";exit 0; } +help=" +########################## $project ############################# +# Usage: +# install java : sudo oraji 'jdk_or_jre_archive_path' +# install java : sudo oraji -i 'jdk_or_jre_archive_path' +# uninstall java : sudo oraji -u jdk|jre +# +# Options: +# [ -v, --version ] : Show version +# [ -h, --help ] : Show help +# [ -i, --install ] : Install from a archive path +# [ -u, --uninstall ] : Uninstall java +# [ -s, --set ] : Set a java version +# [ -c, --check ] : Show the installed java versions +############################################################## +" shopt -s extglob -if [ $EUID -ne 0 ]; then - echo " - *********This script must be run with root privilege********* - *******************Sorry! Abort!***************************** - " - exit 1 -fi +####################################### General functions ###################################### + +err(){ + echo "E: oraji: $@" >> /dev/stderr +} -####################################### Expand path function ###################################### + +chkroot(){ + if [ x$EUID != x0 ]; then + err " + This script must be run with root privilege + *Sorry! Abort!** + " + exit 1 + fi +} expandPath() { local path @@ -68,12 +89,12 @@ expandPath() { printf -v result '%s:' "${resultPathElements[@]}" printf '%s\n' "${result%:}" } -####################################### Expand path function ###################################### +####################################### General functions end ###################################### ################# Path Managing functions ##################################### disableJavaPath(){ jpath="$1" -pat="$(echo "$jpath" |sed 's/[^^]/[&]/g; s/\^/\\^/g')" +pat="$(echo "$jpath" |sed -e 's/[^^]/[&]/g' -e 's/\^/\\^/g')" sed -e "s/^[[:blank:]]*export[[:blank:]]*J2SDKDIR[=]$pat/# &/" \ -e "s/^[[:blank:]]*export[[:blank:]]*J2REDIR[=]$pat/# &/" \ -e "s/^[[:blank:]]*export[[:blank:]]*PATH[=]\$PATH[:]$pat/# &/" \ @@ -95,13 +116,13 @@ sed -e "s/^[[:blank:]]*#[[:blank:]]*\(export[[:blank:]]*J2SDKDIR[=]$pat.*\)/\1/ deleteJavaPath(){ jpath="$1" -[[ ! $jpath =~ ^/usr/lib/jvm/j(dk|re).* ]] && { echo "E: argument to deletePath is invalid"; exit 1; } +[[ ! $jpath =~ ^/usr/lib/jvm/j(dk|re).* ]] && { err "E: argument to deletePath is invalid"; exit 1; } pat="$(echo "$jpath" |sed 's/[^^]/[&]/g; s/\^/\\^/g')" -sed -e "/^[[:blank:]]*export[[:blank:]]*J2SDKDIR[=]$pat/d" \ - -e "/^[[:blank:]]*export[[:blank:]]*J2REDIR[=]$pat/d" \ - -e "/^[[:blank:]]*export[[:blank:]]*PATH[=]\$PATH[:]$pat/d" \ - -e "/^[[:blank:]]*export[[:blank:]]*JAVA_HOME[=]$pat/d" \ - -e "/^[[:blank:]]*export[[:blank:]]*DERBY_HOME[=]$pat/d" \ +sed -e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*J2SDKDIR[=]$pat/d" \ + -e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*J2REDIR[=]$pat/d" \ + -e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*PATH[=]\$PATH[:]$pat/d" \ + -e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*JAVA_HOME[=]$pat/d" \ + -e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*DERBY_HOME[=]$pat/d" \ --in-place /etc/profile } @@ -169,54 +190,69 @@ installJavaPaths(){ . /etc/profile >/dev/null && echo " ***environment variables updated successfully***" || - echo " + err " ---Failed to update environment variables" } +getJName(){ + while IFS= read -r -d $'\0'; do + array+=("$REPLY") + done < <(find /usr/lib/jvm -maxdepth 1 -type d \( -name 'jdk*' -o -name 'jre*' \) -printf "%P\0") + + echo "Choice Version" >>/dev/stderr + for((i=0;i<${#array[@]};i++));do + echo "$i. ${array[$i]}" >>/dev/stderr + done + + arri="" + while [[ ! "$arri" =~ ^[0-9]+$ ]];do + read -r -p "Enter your choice: " arri + done + echo "${array[arri]}" +} + ################# Path Managing functions ##################################### + ####################################### Uninstall function ######################################## uninstall(){ shopt -s extglob jname="$1" - [[ $jname =~ ^(jdk|jre).* || ! -d /usr/lib/jvm/$jname ]] && { echo " - ---Invalid java name/version.---"; exit 2; } - - [[ $jname == "" ]] && - { - if update-alternatives --display java >/dev/null 2>&1;then - jname="$(update-alternatives --display java |grep -oE 'points\s*to\s*/usr/lib/jvm/[^/]*')" - jname="${jname##*/}" - read -e -i "$jname" -p "Enter the java version (with jdk/jre): " jname - else - read -e -i "$jname" -p "Enter the java version (with jdk/jre): " jname - fi - } + [[ $jname == "" ]] && jname="$(getJName)" + #{ + #if update-alternatives --display java >/dev/null 2>&1;then + # jname="$(update-alternatives --display java |grep -oE 'points\s*to\s*/usr/lib/jvm/[^/]*')" + # jname="${jname##*/}" + # read -e -i "$jname" -p "Enter the java version (with jdk/jre): " jname + #else + # read -e -i "$jname" -p "Enter the java version (with jdk/jre): " jname + #fi + #} [[ -d /usr/lib/jvm/"$jname" ]] || { - printf "%s\n" " + err " -----Directory not found: /usr/lib/jvm/$jname----- "; exit 3; } + - - [[ $jname == "" || $jname =~ \/ ]] && echo " + [[ $jname == "" || $jname =~ \/ ]] && err " ------java name can not be empty or contain /------" && exit 4 ##variable jname is safe to use now. patt="^(jdk|jre).*" - [[ $jname =~ $patt ]] || { echo " + [[ $jname =~ $patt ]] || { err " ------Couldn't recognize java name/version------";exit 5; } jgenre="${BASH_REMATCH[1]}" ## whether it's jdk or jre doing=false; for prog in /usr/lib/jvm/"$jname"/bin/*;do progn="${prog##*/}" [[ $progn == *"."* ]] && continue - if [[ -f $prog ]];then + if [[ -f "$prog" ]];then update-alternatives --remove "$progn" "/usr/lib/jvm/$jname/bin/$progn" doing=true fi @@ -233,14 +269,14 @@ uninstall(){ source /etc/profile >/dev/null && echo " ***Removed java environment variables***" || - echo " + err " ---Failed to remove java environment variables" ##We will remove mozilla plugins for only the active java version. if $doing;then rm ~/.mozilla/plugins/libnpjp2.so && echo " ***Mozilla plugin removed***" || - echo " + err " ---Mozilla plugin not found (ignore this error)---" # optional fi @@ -264,7 +300,7 @@ uninstall(){ source /etc/profile >/dev/null && echo " ***Environment variables restored for Fall-Back java***" || - echo " + err " ---Couldn't restore environment variables for Fall-Back java---" [[ $jname =~ ^jdk ]] && ln -sf /usr/lib/jvm/$jname/jre/lib/$arch/libnpjp2.so ~/.mozilla/plugins/ || @@ -272,7 +308,7 @@ uninstall(){ [[ $jname =~ ^jre ]] && ln -sf /usr/lib/jvm/$jname/lib/$arch/libnpjp2.so ~/.mozilla/plugins/ } && echo " ***Mozilla plugin restored for fall back java version***" || - echo " + err " ---Couldn't restore mozilla plugin. Restore it manually if needed. For jdk: ln -sf /usr/lib/jvm/jdk/jre/lib/your_arch/libnpjp2.so ~/.mozilla/plugins/ For jre: ln -sf /usr/lib/jvm/jre/lib/your_arch/libnpjp2.so ~/.mozilla/plugins/" @@ -292,39 +328,110 @@ uninstall(){ } ####################################### Uninstall function ######################################## -[[ $1 == "-u" ]] && { uninstall "$2";exit 0; } -jpath="$1" +####################################### Opt parse ###################################################### +ustall=false +ustall_jname="" +set=false +sname="" +jpath="" +while (( $# > 0 )); do + case "$1" in + -v|--version) + echo "$version_info" + exit 0 + ;; + -h|--help) + echo "$help" + exit 0 + ;; + -c|--check) + echo " + Current Java: + " + update-alternatives --display java 2>&1 |grep 'point' |sed -n 's#.*/\(j\(dk\|re\)[^/]*\)/.*#\1#p' + echo " + Available Java: + " + find /usr/lib/jvm -maxdepth 1 -type d \( -name 'jdk*' -o -name 'jre*' \) -printf "%P\n" + exit 0 + ;; + -u|--ustall) + $set && { err "More than one action is not permitted"; exit 1; } + ustall=true + ustall_jname="$2" + [[ "$jpath" != "" || "$ustall_jname" = -* ]] && { err "Invalid arg for uninstall option"; exit 1; } + chkroot + uninstall "$ustall_jname" + exit 0 + shift + ;; + -s|--set) + $ustall && { err "More than one action is not permitted"; exit 1; } + set=true + if [[ "$2" != "" ]]; then + sname="$2" + shift + else + sname="$(getJName)" + fi + ;; + -i|--install|*) + $ustall && { err "Can't install and uninstall at the same time, choose one."; exit 1; } + $set && { err "Can't set and install at the same time, choose one."; exit 1; } + if [[ "$1" = -i || "$1" = --install ]];then + [[ "$jpath" = "" ]] && jpath="$2" || { err "More than one archive path given"; exit 1; } + shift + else + [[ "$jpath" = "" ]] && jpath="$1" || { err "More than one archive path given"; exit 1; } + fi + [[ "$jpath" = "" ]] && { err "Archive path missing. Usage: oraji -i archive_path"; exit 1; } + esac + shift +done +####################################### Opt parse ###################################################### -#mytty=$(tty) +################## +## check for root +chkroot -[[ $jpath == "" ]] && read -e -p "Enter the java archive path: " jpath && jpath="$(expandPath "$jpath")" +if ! $set; then + [[ $jpath == "" ]] && read -e -p "Enter the java archive path: " jpath && jpath="$(expandPath "$jpath")" -##Validate jpath + ##Validate jpath -[[ ! $jpath =~ .*(jdk|jre).*\.tar\..* ]] && { echo "Failed to recognize archive: $jpath";exit 2;} -[[ -d $jpath ]] && { echo "You must pass an archive file, not a directory";exit 2; } -[[ ! -f $jpath ]] && { echo "No such file: $jpath";exit 2;} + [[ ! $jpath =~ .*(jdk|jre).*\.tar\..* ]] && { err "Failed to recognize archive: $jpath";exit 2;} + [[ -d $jpath ]] && { err "You must pass an archive file, not a directory";exit 2; } + [[ ! -f $jpath ]] && { err "No such file: $jpath";exit 2;} -tmpdir="$(mktemp -d)" + tmpdir="$(mktemp -d)" -tar -xvf "$jpath" -C "$tmpdir" || { echo "Failed to extract archive"; exit 2; } + tar -xvf "$jpath" -C "$tmpdir" || { err "Failed to extract archive"; exit 2; } -cd "$tmpdir"/j* || { echo "Couldn't find java directory"; exit 3; } -jname="${PWD##*/}" -patt="^(jdk|jre).*" -[[ $jname =~ $patt ]] -jgenre="${BASH_REMATCH[1]}" ## whether it's jdk or jre + cd "$tmpdir"/j* || { err "Couldn't find java directory"; exit 3; } + jname="${PWD##*/}" -[[ $jname != "" && $tmpdir != "" ]] || -{ echo " ------Variable jname/tmpdir can not be empty----- -Please file a bug report with all available terminal output and info at: -$bug_report -"; exit 4; } + [[ $jname != "" && $tmpdir != "" ]] || + { err " + -----Variable jname/tmpdir can not be empty----- + Please file a bug report with all available terminal output and info at: + $bug_report + "; exit 4; } -mkdir -p /usr/lib/jvm/"$jname" -mv -f ./* -t /usr/lib/jvm/"$jname" + mkdir -p /usr/lib/jvm/"$jname" + mv -f ./* -t /usr/lib/jvm/"$jname" +else + jname="$sname" +fi +#### File copied successfully + +while [[ "$jname" = "" ]]; do + read -p "Enter the java version (with jdk/jre): " jname +done + +patt="^(jdk|jre).*" +[[ $jname =~ $patt ]] || { err "Couldn't recognize java name/version";exit 5; } +jgenre="${BASH_REMATCH[1]}" ## whether it's jdk or jre pr=22222 ## priority @@ -342,6 +449,9 @@ if update-alternatives --display java >/dev/null 2>&1;then fi ##Now install all available binaries with `update-alternatives --install` and set them to be used + +[[ -d "/usr/lib/jvm/$jname" ]] || { err "No such directory: /usr/lib/jvm/$jname"; exit 1; } + for prog in /usr/lib/jvm/"$jname"/bin/*;do progn="${prog##*/}" [[ $progn == *"."* ]] && continue @@ -384,7 +494,7 @@ if echo "$(java -version 2>&1)" |grep -qsi "java version.*$jv" ; then fi (( $? == 0)) && echo " ***Managed mozilla plugin successfully***" || - echo " + err " ---Failed to manage mozilla plugin. If you think it's necessary run ln -sf /usr/lib/jvm/$jname/jre/lib/your-system-architecture-here/libnpjp2.so ~/.mozilla/plugins/ #For jdk and ln -sf /usr/lib/jvm/$jname/lib/your-system-architecture-here/libnpjp2.so ~/.mozilla/plugins/ #For jre @@ -399,6 +509,6 @@ if echo "$(java -version 2>&1)" |grep -qsi "java version.*$jv" ; then (To populate environment variables immediately run 'source /etc/profile' manually) " else - echo "-----Failed to install java. Run 'java -version' manually to confirm.----" + err "-----Failed to install java. Run 'java -version' manually to confirm.----" fi diff --git a/oraji.1 b/oraji.1 index c61bfcb..fc67aa6 100644 --- a/oraji.1 +++ b/oraji.1 @@ -6,7 +6,7 @@ oraji - Oracle Java Installer .SH "SYNOPSIS" .B oraji .I path/to/the/jdk_or_jre_archive -.IP "oraji [-u, -v]" +.IP "oraji [-i, -u, -c, -s, -h, -v]" .SH DESCRIPTION Oracle Java (jdk/jre) installler script for Linux. It simplifies the installation of Oracle Java from a downloaded source (tar.gz). It also maintains consistency among multiple versions of Java (jdk/jre) installed with this script. Uninstalling any Java version installed with this script will fall back to the next most recently installed (with this script) version of Java. @@ -32,9 +32,11 @@ Uninstalls Java installed with oraji. An extra argument can be passed with this .B oraji -u and confirm the version, it's safe. .IP "-s, --set" -Takes a second parameter as java version in the form: jdk|jre (e.g jdk1.8.0_92) and sets this version of java as the active one. +Takes an optional second parameter as java version in the form: jdk|jre (e.g jdk1.8.0_92) and sets this version of java as the active one. If the second parameter is not given, a selection menu will be presented. +.IP "-c, --check" +Display the available java versions as well as the currently active one. .IP "-v, --version" -shows the version number. +Shows version info. .IP "-h, --help" Show help