2.1.2 Downloading and Installing Java§
As experienced Java developers transitioning to Clojure, setting up your development environment is crucial. Java Development Kit (JDK) is a prerequisite for running Clojure, as Clojure runs on the Java Virtual Machine (JVM). This section will guide you through downloading and installing the JDK on different operating systems, ensuring you have the right tools to start your Clojure journey.
Choosing the Right JDK§
Before diving into the installation process, it’s important to choose the right JDK distribution. There are several options available:
- Oracle JDK: The official JDK from Oracle, often used in enterprise environments. It offers commercial support and long-term support (LTS) versions.
- OpenJDK: The open-source implementation of the Java Platform, Standard Edition. It is widely used and supported by the community.
- AdoptOpenJDK: Now part of the Eclipse Foundation as Adoptium, it provides prebuilt OpenJDK binaries with a focus on performance and security.
Selecting the Appropriate Version§
When choosing a JDK version, consider the following:
- LTS Versions: These versions receive updates and support for an extended period, making them ideal for production environments. Examples include Java 11 and Java 17.
- Standard Releases: These are released every six months and may not have long-term support. They are suitable for testing new features.
For Clojure development, it’s recommended to use an LTS version to ensure stability and compatibility.
Downloading and Installing Java on Windows§
Step 1: Download the JDK§
- Visit the Official JDK Website: Go to the Oracle JDK or AdoptOpenJDK website.
- Select the Version: Choose the appropriate JDK version (e.g., Java 11 LTS) and click on the download link for Windows.
- Choose the Installer: Download the
.exe
installer for Windows.
Step 2: Install the JDK§
- Run the Installer: Double-click the downloaded
.exe
file to start the installation process.
- Follow the Installation Wizard: Accept the license agreement and choose the installation directory. The default path is usually
C:\Program Files\Java\jdk-<version>
.
- Complete the Installation: Click “Next” and “Finish” to complete the installation.
Step 3: Set Environment Variables§
- Open System Properties: Right-click on “This PC” or “My Computer” and select “Properties”.
- Access Environment Variables: Click on “Advanced system settings” and then “Environment Variables”.
- Add JAVA_HOME: Under “System variables”, click “New” and add
JAVA_HOME
with the path to your JDK installation (e.g., C:\Program Files\Java\jdk-<version>
).
- Update PATH Variable: Find the “Path” variable, click “Edit”, and add
%JAVA_HOME%\bin
.
Step 4: Verify the Installation§
- Open Command Prompt: Press
Win + R
, type cmd
, and press Enter.
- Check Java Version: Type
java -version
and javac -version
to verify the installation.
java -version
javac -version
Downloading and Installing Java on macOS§
Step 1: Download the JDK§
- Visit the Official JDK Website: Go to the Oracle JDK or AdoptOpenJDK website.
- Select the Version: Choose the appropriate JDK version (e.g., Java 11 LTS) and click on the download link for macOS.
- Choose the Installer: Download the
.dmg
installer for macOS.
Step 2: Install the JDK§
- Open the Installer: Double-click the downloaded
.dmg
file to mount it.
- Run the Package Installer: Double-click the
.pkg
file to start the installation process.
- Follow the Installation Wizard: Accept the license agreement and follow the prompts to complete the installation.
Step 3: Set Environment Variables§
- Open Terminal: Press
Cmd + Space
, type Terminal
, and press Enter.
- Edit Profile: Open your profile file (
~/.bash_profile
, ~/.zshrc
, or ~/.bashrc
) in a text editor.
- Add JAVA_HOME: Add the following line to set the
JAVA_HOME
environment variable:
export JAVA_HOME=$(/usr/libexec/java_home)
- Update PATH Variable: Add the following line to update the
PATH
variable:
export PATH=$JAVA_HOME/bin:$PATH
- Apply Changes: Save the file and run
source ~/.bash_profile
(or the equivalent for your shell) to apply the changes.
Step 4: Verify the Installation§
- Check Java Version: In Terminal, type
java -version
and javac -version
to verify the installation.
java -version
javac -version
Downloading and Installing Java on Linux§
Step 1: Download the JDK§
- Visit the Official JDK Website: Go to the Oracle JDK or AdoptOpenJDK website.
- Select the Version: Choose the appropriate JDK version (e.g., Java 11 LTS) and click on the download link for Linux.
- Choose the Package: Download the
.tar.gz
archive or the .deb
/.rpm
package for your distribution.
Step 2: Install the JDK§
For Debian-based systems (e.g., Ubuntu):
- Open Terminal: Press
Ctrl + Alt + T
to open Terminal.
- Install the Package: Use
dpkg
to install the downloaded package:
sudo dpkg -i <package-name>.deb
For Red Hat-based systems (e.g., Fedora, CentOS):
- Open Terminal: Press
Ctrl + Alt + T
to open Terminal.
- Install the Package: Use
rpm
to install the downloaded package:
sudo rpm -ivh <package-name>.rpm
For other distributions:
- Extract the Archive: Use
tar
to extract the .tar.gz
archive:
tar -xvf <package-name>.tar.gz
- Move to /opt: Move the extracted directory to
/opt
:
sudo mv jdk-<version> /opt/
Step 3: Set Environment Variables§
- Edit Profile: Open your profile file (
~/.bashrc
, ~/.zshrc
, or ~/.bash_profile
) in a text editor.
- Add JAVA_HOME: Add the following line to set the
JAVA_HOME
environment variable:
export JAVA_HOME=/opt/jdk-<version>
- Update PATH Variable: Add the following line to update the
PATH
variable:
export PATH=$JAVA_HOME/bin:$PATH
- Apply Changes: Save the file and run
source ~/.bashrc
(or the equivalent for your shell) to apply the changes.
Step 4: Verify the Installation§
- Check Java Version: In Terminal, type
java -version
and javac -version
to verify the installation.
java -version
javac -version
Troubleshooting Common Issues§
- Java Version Conflicts: Ensure that the
JAVA_HOME
and PATH
variables point to the correct JDK version.
- Permission Errors: Use
sudo
for commands that require administrative privileges.
- Incomplete Installation: Re-download the installer if the installation fails.
Try It Yourself§
To solidify your understanding, try the following:
- Experiment with Different JDK Versions: Install both Java 11 and Java 17, and switch between them using the
JAVA_HOME
variable.
- Automate the Installation: Write a shell script to automate the JDK installation and environment variable setup.
Summary and Key Takeaways§
- Choose the Right JDK: Select an LTS version for stability and long-term support.
- Follow Platform-Specific Instructions: Each operating system has unique steps for installation.
- Verify Your Installation: Always check the Java version to ensure the installation was successful.
By following these steps, you’ll have a robust Java environment ready for Clojure development. Now that you’ve set up Java, you’re one step closer to mastering Clojure’s functional programming paradigm.
Quiz Time!§