How to switch Java version with direnv in terminal on Mac

I'm sure there are a few other people who are developing Java on a Mac, running commands like mvn from a terminal (rather than an IDE), and who love direnv.

For such a person, it would be convenient to be able to specify the version as follows with .envrc (which can be edited with direnv edit .), isn't it?

use java 8

Define the following function in ~ / .direnvrc.

use_java() {
    if [ "$#" -ne 1 ]; then
    echo "usage: use java VERSION" >&2
    return 1
  fi
  local v
  v="$1"
  if [ "$v" -le "8" ]; then
    v="1.$v"
  fi
  export JAVA_HOME="$(/usr/libexec/java_home -v "$v")"
}

--direnv can call the  Ľuse_xxx function in the form of  Ľuse xxx --Define the environment variable $ JAVA_HOME to switch Java versions --On Mac, refer to the Home directory of each Java installed with / usr / libexec / java_home

It just implements that.

Recommended Posts

How to switch Java version with direnv in terminal on Mac
How to switch Java versions on Mac
How to switch Java in the OpenJDK era on Mac
How to check Java installed on Mac
Switch java with direnv
How to call functions in bulk with Java reflection
How to lower java version
How to switch Tomcat context.xml with WTP in Eclipse
As of April 2018 How to get Java 8 on Mac
Notes on how to use regular expressions in Java
How to decompile apk file to java source code with MAC
How to dynamically switch JDK when building Java in Gradle
How to use mysql with M1 mac Docker preview version
[Java] How to update Java on Windows
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
How to uninstall if you have updated to Java 9 on your Mac
How to automatically operate a screen created in Java on Windows
Android: How to deal with "Could not determine java version from '10 .0.1'"
Project facet Java version 13 is not supported. How to deal with
How to call with One Touch (without confirmation) on Android (Java)
How to encrypt and decrypt with RSA public key in Java
How to get JDK 11 on your mac in a comfortable way
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
How to switch thumbnail images with JavaScript
How to install Eclipse (Photon) on Mac
How to do base conversion in Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
How to install the legacy version [Java]
How to get the date in java
How to switch between multiple Java versions
[Personal memo] How to interact with a random number generator in Java
How to get the value after "_" in Windows batch like Java -version
[Java] How to test for null with JUnit
Easily switch Java versions with alias on macOS
How to display a web page in Java
Run Edge (Chromium version) on Mac with Selenium
How to use Java framework with AWS Lambda! ??
How to get Class from Element in Java
How to use Java API with lambda expression
How to hide null fields in response in Java
Switch between multiple versions of Java on Mac
[Java] How to switch from open jdk to oracle jdk
How to use nfs protocol version 2 with ubuntu 18.04
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to use nginx-ingress-controller with Docker for Mac
[Ruby on Rails] How to install Bootstrap in Rails
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
How to run NullpoMino 7.5.0 on Ubuntu 20.04.1 64bit version
How to check Rails commands in the terminal
Notes on how to write comments in English
I tried running Java on a Mac terminal