Quickly switch between different JDKs on the Mac system

preamble

JDK is currently updated super fast, and a version is released in March and September every year. Currently, JDK19 has been released. If you want to experience the functional differences of different JDKs in one environment and switch easily, you can refer to this article

command line switch

If you manually modify the environment variable ( ~/.bash_profile or ~/.zshrc ) every time you switch versions , and then execute the source command to make the modification take effect, it is still troublesome

You can consider using the alias command to configure shortcuts. The specific implementation is as follows, add the following configuration in the ~/.bash_profile file

### JDK definition
JAVA_08=$(/usr/libexec/java_home -v 1.8)
JAVA_11=$(/usr/libexec/java_home -v 11)
JAVA_17=$(/usr/libexec/java_home -v 17)
JAVA_19=$(/usr/libexec/java_home -v 19)

### alias
alias java8='echo "export JAVA_HOME=$JAVA_08" >> ~/.bash_profile && source ~/.bash_profile && echo "switch to Java8"'
alias java11='echo "export JAVA_HOME=$JAVA_11" >> ~/.bash_profile && source ~/.bash_profile && echo "switch to Java11"'
alias java17='echo "export JAVA_HOME=$JAVA_17" >> ~/.bash_profile && source ~/.bash_profile && echo "switch to Java17"'
alias java19='echo "export JAVA_HOME=$JAVA_19" >> ~/.bash_profile && source ~/.bash_profile && echo "switch to Java19"'

After the configuration is complete, execute source ~/.bash_profile

If you want to switch to JDK17, just enter the alias java17 directly on the command line

  Use alias to view the list of currently existing aliases

/usr/libexec/java_home is used in the above configuration file . After Mac OSX 10.5,  /usr/libexec/ add a java_home file under the path. This is the tool used by Mac system to manage JAVA_HOME

At present, switching the JDK version takes effect globally . You can also get the latest JAVA_HOME by opening a new Terminal. After switching several times, I found that the last few lines of the ~/.bash_profile file are all JAVA_HOME, and the last one takes effect without affecting the function. But let's optimize it, consider using the sed command to delete the last few lines, and then add the latest value

The optimized configuration is as follows

### JDK definition
JAVA_08=$(/usr/libexec/java_home -v 1.8)
JAVA_11=$(/usr/libexec/java_home -v 11)
JAVA_17=$(/usr/libexec/java_home -v 17)
JAVA_19=$(/usr/libexec/java_home -v 19)
THE_F=~/.bash_profile

### alias
alias java8='sed -i "" "/export JAVA_HOME=\//d" $THE_F && echo "export JAVA_HOME=$JAVA_08" >> $THE_F && source $THE_F && echo "switch to Java8"'
alias java11='sed -i "" "/export JAVA_HOME=\//d" $THE_F && echo "export JAVA_HOME=$JAVA_11" >> $THE_F && source $THE_F && echo "switch to Java11"'
alias java17='sed -i "" "/export JAVA_HOME=\//d" $THE_F && echo "export JAVA_HOME=$JAVA_17" >> $THE_F && source $THE_F && echo "switch to Java17"'
alias java19='sed -i "" "/export JAVA_HOME=\//d" $THE_F && echo "export JAVA_HOME=$JAVA_19" >> $THE_F && source $THE_F && echo "switch to Java19"'

After switching several times again, I found that everything was normal and solved perfectly

reference documents

Detailed explanation of unalias command

Mac sed command invalid command code error

Guess you like

Origin blog.csdn.net/u013481793/article/details/127169360