Recently, I need to learn Java. Due to personal habits, I just want to use the terminal for development instead of using an IDE. But I never expected that I would be tricked by the official website once, and the downloaded Java could not be developed normally. So I wrote this article to explain how to configure it. It’s very simple, just find the right official website.
The first thing is to find the official website, but it is the developer’s official website, not the Java official website, the address is: https://dev.java, the page is as follows:
The difference between Java and the developer’s official website is like the difference between Apple’s official website and developers. The former is for ordinary users, while the developer’s official website is for developers.
If you click “Download Java” directly on the official website (below), the downloaded Java can only run the program, but cannot compile the program, which means that it is useless for developers to download it. And the following error will be reported:
The operation couldn’t be completed. Unable to locate a Java Runtime that supports javac.
So where can I download the Java SDK (that is, the Java development environment) that developers need?
The simplest solution is to click directlyhttps://www.oracle.com/java/technologies/downloads/#jdk19-mac, download the required version from Oracle’s official website (note that Oracle’s Java cannot be used only for personal use). Or to make it more troublesome (this method is said to avoid problems with the download address), click “Developer Resources” in the picture above or click directly to enter the developer’s official website https://dev.java, and then click “Download Java 19” in the picture. At this time, the page is as follows:
Then click the “Download Java at Oracle.com” button to jump to Oracle’s official website to download the required version.
If you downloaded the dmg version, just click on it and keep going to the next step. If you downloaded “Compressed Archive”, you need to use the following command to expand it first:
tar xzf package name.tar.gz
Then add an environment variable to point to the directory of the expanded JDK.
export JAVA_HOME=/Users/javauser/jdk/jdk-19.jdk/Contents/Home
Then update the PATH
environment variable so that you can use Java directly without entering the relative/absolute path every time:
export PATH=$JAVA_HOME/bin:$PATH
Then use the following command to check if everything is OK:
which java
It’s really simple, but I didn’t expect to be fooled by the two versions. I checked and found that some people have encountered it, so I wrote this article hoping to help people in need ~