Skip to content

Instantly share code, notes, and snippets.

@marnovo
Forked from trmaphi/changeJDK.bash
Created October 20, 2020 09:17
Show Gist options
  • Select an option

  • Save marnovo/e9e0e8989a3228d6c9ddda82768f934a to your computer and use it in GitHub Desktop.

Select an option

Save marnovo/e9e0e8989a3228d6c9ddda82768f934a to your computer and use it in GitHub Desktop.

Revisions

  1. @trmaphi trmaphi revised this gist Apr 13, 2019. No changes.
  2. @trmaphi trmaphi revised this gist Apr 8, 2019. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions changeJDK.bash
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,6 @@
    JDKS_DIR="/Library/Java/JavaVirtualMachines"
    JDKS=( $(ls ${JDKS_DIR}) )
    JDKS_STATES=()
    DEFAULT_JDK_DIR=""
    DEFAULT_JDK=""

    # Map state of JDK
    for (( i = 0; i < ${#JDKS[@]}; i++ )); do
    @@ -16,8 +14,12 @@ for (( i = 0; i < ${#JDKS[@]}; i++ )); do
    echo "${i} ${JDKS[$i]} ${JDKS_STATES[$i]}"
    done

    # OPTION for default jdk and set variables
    # Declare variables
    DEFAULT_JDK_DIR=""
    DEFAULT_JDK=""
    OPTION=""

    # OPTION for default jdk and set variables
    while [[ ! "$OPTION" =~ ^[0-9]+$ || OPTION -ge "${#JDKS[@]}" ]]; do
    read -p "Enter Default JDK: " OPTION
    if [[ ! "$OPTION" =~ ^[0-9]+$ ]]; then
    @@ -28,6 +30,7 @@ while [[ ! "$OPTION" =~ ^[0-9]+$ || OPTION -ge "${#JDKS[@]}" ]]; do
    echo "Out of index"
    fi
    done

    DEFAULT_JDK_DIR="${JDKS_DIR}/${JDKS[$OPTION]}"
    DEFAULT_JDK="${JDKS[$OPTION]}"

    @@ -38,7 +41,7 @@ for (( i = 0; i < ${#JDKS[@]}; i++ )); do
    fi
    done

    #Enable default jdk
    # Enable default jdk
    if [[ -f "${DEFAULT_JDK_DIR}/Contents/Info.plist.disable" ]]; then
    sudo mv "${DEFAULT_JDK_DIR}/Contents/Info.plist.disable" "${DEFAULT_JDK_DIR}/Contents/Info.plist"
    echo "Enable ${DEFAULT_JDK} as default JDK"
  3. @trmaphi trmaphi created this gist Apr 7, 2019.
    45 changes: 45 additions & 0 deletions changeJDK.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/usr/bin/env bash

    JDKS_DIR="/Library/Java/JavaVirtualMachines"
    JDKS=( $(ls ${JDKS_DIR}) )
    JDKS_STATES=()
    DEFAULT_JDK_DIR=""
    DEFAULT_JDK=""

    # Map state of JDK
    for (( i = 0; i < ${#JDKS[@]}; i++ )); do
    if [[ -f "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist" ]]; then
    JDKS_STATES[${i}]=enable
    else
    JDKS_STATES[${i}]=disable
    fi
    echo "${i} ${JDKS[$i]} ${JDKS_STATES[$i]}"
    done

    # OPTION for default jdk and set variables
    OPTION=""
    while [[ ! "$OPTION" =~ ^[0-9]+$ || OPTION -ge "${#JDKS[@]}" ]]; do
    read -p "Enter Default JDK: " OPTION
    if [[ ! "$OPTION" =~ ^[0-9]+$ ]]; then
    echo "Sorry integers only"
    fi

    if [[ OPTION -ge "${#JDKS[@]}" ]]; then
    echo "Out of index"
    fi
    done
    DEFAULT_JDK_DIR="${JDKS_DIR}/${JDKS[$OPTION]}"
    DEFAULT_JDK="${JDKS[$OPTION]}"

    # Disable all jdk
    for (( i = 0; i < ${#JDKS[@]}; i++ )); do
    if [[ -f "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist" ]]; then
    sudo mv "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist" "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist.disable"
    fi
    done

    #Enable default jdk
    if [[ -f "${DEFAULT_JDK_DIR}/Contents/Info.plist.disable" ]]; then
    sudo mv "${DEFAULT_JDK_DIR}/Contents/Info.plist.disable" "${DEFAULT_JDK_DIR}/Contents/Info.plist"
    echo "Enable ${DEFAULT_JDK} as default JDK"
    fi