- Check the available jdks
update-alternatives --config java
- Copy the selection path without
/bin/java
/usr/lib/jvm/java-17-openjdk-amd64
- Edit the file
nano ~/.bashrc
- Add the following line
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
- Save the file (
Ctrl + O
, thenEnter
) and exit (Ctrl + X
) - Apply the changes by running
source ~/.bashrc
- Ensure that the JAVA_HOME variable is now set correctly by running
echo $JAVA_HOME
- It should output
/usr/lib/jvm/java-17-openjdk-amd64
To install Java via the Windows Command Prompt, you'll need to download the Java Development Kit (JDK) and set it up. Here’s a step-by-step guide:
- Go to OpenJDK page for a free version.
- Choose the version you need and download the Windows installer (usually a
.exe
file).
- Run the downloaded installer.
- Follow the installation prompts. By default, it installs in
C:\Program Files\Java\jdk-<version>
.
- Open Command Prompt: Press
Win + R
, typecmd
, and hitEnter
. - Set JAVA_HOME:
- Type the following command (replace
<version>
with your installed JDK version):setx JAVA_HOME "C:\Program Files\Java\jdk-<version>"
- Type the following command (replace
- Update the PATH Variable:
- Add the JDK
bin
directory to your PATH by typing:setx PATH "%PATH%;%JAVA_HOME%\bin"
- Add the JDK
- Close the Command Prompt and reopen it to ensure the environment variables are updated.
- Type the following command to check the Java installation:
You should see the installed version of Java.
java -version
You can create a simple Java program to test if everything works:
- Open a text editor and write a simple Java program (e.g.,
HelloWorld.java
):public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
- Save it in a directory (e.g.,
C:\Java
). - Open the Command Prompt, navigate to the directory, and compile the program:
cd C:\Java javac HelloWorld.java
- Run the program:
java HelloWorld
If you see "Hello, World!" printed on the screen, you’ve successfully installed and set up Java!
To install Java 21 on a Mac using Homebrew, follow these steps:
If you haven't already installed Homebrew, open Terminal and enter:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Open Terminal and run the following command to install OpenJDK 21:
brew install openjdk@21
-
After the installation completes, Homebrew will display the path to the Java 21 installation, typically something like:
/opt/homebrew/opt/openjdk@21
To ensure that Java 21 is recognized as the default Java version, set the JAVA_HOME
environment variable.
-
Add the following line to your shell profile file:
- For Zsh (default on macOS Catalina and newer): edit
~/.zshrc
. - For Bash: edit
~/.bash_profile
or~/.bashrc
.
export JAVA_HOME=/opt/homebrew/opt/openjdk@21 export PATH="$JAVA_HOME/bin:$PATH"
- Save the file (
Ctrl + O
, thenEnter
) and exit (Ctrl + X
)
- For Zsh (default on macOS Catalina and newer): edit
-
Reload the profile configuration by running:
source ~/.zshrc # or source ~/.bash_profile, depending on your shell
Finally, confirm that Java 21 is installed and set as the default version:
java -version
You should see output like:
java version "21.0.x" 2023-09-19 LTS
Java 21 is now installed and configured on your Mac using Homebrew!