Skip to content

Instantly share code, notes, and snippets.

@guozi
Forked from tomysmile/brew-java-and-jenv.md
Created March 6, 2019 02:06
Show Gist options
  • Save guozi/84d5420e2af64fe7e015c0e7e0bb2f7d to your computer and use it in GitHub Desktop.
Save guozi/84d5420e2af64fe7e015c0e7e0bb2f7d to your computer and use it in GitHub Desktop.

Revisions

  1. @tomysmile tomysmile renamed this gist Apr 26, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @tomysmile tomysmile revised this gist Apr 26, 2016. 1 changed file with 49 additions and 1 deletion.
    50 changes: 49 additions & 1 deletion brew-java.md
    Original file line number Diff line number Diff line change
    @@ -36,4 +36,52 @@ If you run into permission issue, add sudo at the beginning of the above command

    As of today, Java 8 is the latest stable. Run the following command to install Java 8:

    ```brew cask install java```
    ```brew cask install java```

    These two JDKs will be installed at the following directories. Your JDKs’ minor and patch versions might be different.

    ```
    /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/
    /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/
    ```

    Enter jEnv

    Now it is time to install jEnv:

    ```brew install jenv```

    Add the following lines to ~/.bash_profile. This will initialize jEnv.

    ```
    # Init jenv
    if which jenv > /dev/null; then eval "$(jenv init -)"; fi
    ```

    jEnv doesn’t install JDKs, so we have to tell jEnv where to look for them. Type these commands to register JDKs in jEnv (replace the minor and patch versions with yours):

    ```sh
    jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/
    jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/
    ```

    After that, run this command to list all registered JDKs:

    ```jenv versions```

    The version with an asterisk is the active version.

    In my case, I need to keep JDK 7 as my default version, so I set the global version to 1.7:

    ```jenv global oracle64-1.7.0.79```

    And in my project, I set the local JDK version to 1.8:

    ```sh
    cd <my project>
    jenv local oracle64-1.8.0.66
    ```

    The above command will create a .java-version file at project root. Its content is the version I just picked for this project:

    ```oracle64-1.8.0.66```
  3. @tomysmile tomysmile revised this gist Apr 26, 2016. 1 changed file with 24 additions and 4 deletions.
    28 changes: 24 additions & 4 deletions brew-java.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    Use HomeBrew.
    Install HomeBrew first

    ```sh
    brew update
    brew tap caskroom/cask
    brew install brew-cask
    ```
    @@ -12,8 +13,27 @@ brew unlink brew-cask
    brew install brew-cask
    ```

    And then finally:
    upgrade brew:

    ```brew cask install java```
    ```brew update && brew upgrade brew-cask && brew cleanup && brew cask cleanup```

    If you want to manage multiple versions of Java on your mac, consider using jenv.
    Install Java 7 and 8

    People on the Stackoverflow cautioned not to install 8 until 7 is installed. So we are going to install JDK 7 first.

    Unlike other version managers such as NVM, jEnv itself doesn’t install JDKs. You have to do it yourself. Luckily, Homebrew Cask made this task really easy. But before doing that, let’s check if we already have JDK 7 installed by Homebrew Cask:

    ```sh
    brew tap caskroom/versions
    brew cask info java7
    ```

    install Java 7:

    ```brew cask install java7```

    If you run into permission issue, add sudo at the beginning of the above command.

    As of today, Java 8 is the latest stable. Run the following command to install Java 8:

    ```brew cask install java```
  4. @tomysmile tomysmile renamed this gist Apr 25, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @tomysmile tomysmile created this gist Apr 25, 2016.
    19 changes: 19 additions & 0 deletions brew-java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    Use HomeBrew.

    ```sh
    brew tap caskroom/cask
    brew install brew-cask
    ```

    If you get the error "already installed", follow the instructions to unlink it, then install again:

    ```sh
    brew unlink brew-cask
    brew install brew-cask
    ```

    And then finally:

    ```brew cask install java```

    If you want to manage multiple versions of Java on your mac, consider using jenv.