MAC configuration JDK operating environment


foreword

As a JAVA developer, it is inevitable to configure the JDK operating environment after buying a new computer. This is my practical record of configuring JDK after buying a MAC. I hope it can be helpful to friends who use MAC.


1. Prepare the installation package

Apple's macbook currently has two common chips, one is an intel chip, and the other is an Apple Silicon chip. In order to better reflect the performance of different chips, various development toolkits provide different implementations, so please choose the appropriate toolkit when installing the development tools. Since the notebook demonstrated in this article is based on the mac book M1 chip, the recommended toolkit is as follows:

JDK: Zulu official website JDK download
Zulu official website JDK download
In order to facilitate the configuration of environment variables, I downloaded the file in .tar.gz format

Two, configure JAVA_HOME

1. Unzip the JDK to the specified folder

I put JDK and other development tools under the resource library of the system, that is, under the Library of the root directory:
1. Open the terminal. At this time, it is in the user directory by default. Enter pwdthe command to verify the path;
2. Enter cd /directly to switch to In the root directory;
3. Enter lsthe command to see the folders in the root directory, and ls -athe command can also see hidden folders. At this time, we can see the Library directory we are looking for;
4. Create a folder for storing JDK in the Library directory, command as follows:

sudo mkdir Java
sudo mkdir JavaVirtualMachines

5. Move the downloaded JDK package to this path, and decompress it;
( Here is a quick way to copy the path :)
Tips
Move:

sudo mv  下载好的文件存放点的路径   /Library/Java/JavaVirtualMachines

Unzip:

tar -zxvf  JDK包

After decompression, switch to the decompressed jdk directory, because we want to use the bin in the zulu-8.jdk folder to run, so we can move this package to the outermost layer. The method of moving is the same as above. Of course, if you don’t move, just configure It is also possible to correctly assign to the bin directory under this file when JAVA_HOME is used.

2. Configure the environment

1. Open a new terminal window, enter ls -a to check whether .bash_profile exists, and create a new one if it does not exist

touch .bash_profile

Screenshot of user directory
2. Edit the .bash_profile file. vim .bash_profile
After entering the edit mode, press ito start adding content.
After the end of the edit mode, press ESC
to save the input. :wq!
Do not save the input :q!
. Check whether the .bash_profile file is editable and easy to usecat .bash_profile

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
PATH=$JAVA_HOME/bin:$PATH
export PATH 
export CLASSPATH

3. Reload .bash_profile to ensure that the configured environment variables take effect source .bash_profile
Check whether the JDK is configured properlyjava -verison
JDK version information

3. Frequently asked questions

1. Limited access problem

If the following prompt appears after entering the java -version command, please go to System Settings - Privacy and Security to allow the use of the file

Authorization interface
insert image description here

2. Restart the terminal environment variable invalid

There is a problem that the terminal environment variable fails after restarting the terminal, which means that the environment variable under zshell does not take effect. The solution is to find the .zshrc file in the user directory and add it at the end. If there is no .zshrc file, manual source $HOME/.bash_profile
creation may not take effect. It is recommended to install oh- my-zsh tool, recommended tutorial: oh-my-zsh installation

Summarize

In fact, after downloading the JDK, you can directly use the decompression tool to decompress it in the download directory and move the needed zulu-8.jdk to the /Library/Java/JavaVirtualMachines directory, but the command line will not only be used on the mac It is more commonly used in the process of server operation and maintenance. I hope that the above operations can make everyone quickly familiar with the command line.

Guess you like

Origin blog.csdn.net/only_xing/article/details/131077447