Mac configure java environment variables

Mac configure java environment variables

1. Clear definition

  • JRE: (Java Runtime Environment), java runtime environment. Including Java Virtual Machine (JVM Java Virtual Machine) and the core class libraries required for Java programs, etc. If you want to run a developed Java program, you only need to install JRE on your computer.

  • JDK: (Java Development Kit Java) Development Kit. JDK is provided for Java developers to use, which includes Java development tools, including JRE. So after installing the JDK, there is no need to install the JRE separately.

  • "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"This is the running path of JRE.

  • "/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home" This is the running path of the JDK.

The java environment variables we want to configure need to go to the java official website to download the jdk dmg file. According to the different Mac processors, select the jdk file suitable for the processor. I am demonstrating here using an Intel chip processor.

2. Install Jdk

Double-click the downloaded jdk file to install. Keep clicking Continue or Next during the installation process to install successfully.

3. Configure environment variables

  • First find the jdk installation path. You can refer to the jdk running path mentioned earlier to find it.

  • You can also use the system's resource library to find the jdk path. The specific method is as follows:

First, find the Finder icon, click on the Finder, click on the Go menu, and select Go to Folder.
Please add image description

Then, enter /Library/Java/ in the dialog box and press enter to enter the Java file in the resource library, then enter the JavaVirtualMachines file. After entering, you can select the jdk version. After selecting the version, enter the Contents file and Home in sequence. file, you can find the bin file of jdk.
Please add image description
Please add image description

After finding the bin file, right-click on the bin file, then press and hold option to see that the copy becomes, copy bin as the path name, select this option to copy the path.

Please add image description

Before formal configuration, you can check the following existing environment variables and enter them in the terminal:

echo $PATH

What follows the command line is the current environment variable. Enter the following command line:

vi  .bash_profile

Start configuring environment variables. Enter this command to enter an interface. Then tap the i key on the keyboard. Only by tapping can you enter the text input mode.

Please add image description

Enter the following code in text input mode

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home  
PATH=$JAVA_HOME/bin:$PATH  //给环境变量赋值
export JAVA_HOME          //导出使其生效
export PATH 

Note that the value of JAVA_HOME above must be the jdk path on your own computer. The difference from the /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home/bin path just now is that the path after JAVA_HOME is missing /bin. Then add /bin after PATH.

Please add image description

To exit and save environment variables, press the ESC key first to return to named line mode. Enter: wq to exit and save environment variables.

Please add image description

Enter the following command line in the terminal to make the environment variables take effect:

source  .bash_profile

Use the echo $PATH command again to check whether the environment variables have been added. If our environment variables are more than the jdk path we just configured, it means that the configuration is successful, but it is not over yet.

Please add image description

4. Must read

  • Since the environment variables we configured are in bash_profile, when the terminal reopens a new window or restarts the terminal, the configured environment variables will not take effect. You need to re-execute the following command to take effect:
source ~/.bash_profile
  • The reason is that the environment variables we configured are in bash, and the default shell used by the terminal is zsh, so the solution is to add it in the .zshrc file
source ~/.bash_profile  //此步类似于在使用zsh时的shell时提前执行这句命令,很像提前加载(仅个人理解)
  • If you don’t know what type of shell you are using at this time, you can use the following command to check:
echo $SHELL

Please add image description

As shown in the figure, /bin/zsh means that the zsh shell is used at this time.

  • First enter the home directory
cd ~
  • Then show hidden files and find the .zshrc folder
ls -a
  • Edit file
sudo vim ./zshrc
  • Press i to enter edit mode and add the following code
source ~/.bash_profile

Please add image description

  • Press the Esc key to exit the editing state input
:wq
  • The above codes mean save and exit (write & quit)

Please add image description

  • During this process, you need to enter the power-on password, and to hide the input, you cannot see the input process. Just press enter if you input it correctly.

  • Finally restart the terminal and enter:

echo $PATH
或
env
  • Both methods can check whether the environment variable is added successfully.

Finally, the java environment variable is truly added successfully.

Guess you like

Origin blog.csdn.net/qq_56866000/article/details/133978910