At times you might download the Java JDK built tar.gz file by yourself and would like to make use of it as the Java version for your Mac Terminal. In such a case you can set the JAVA_HOME in the .zshrc profile file by following the below steps.
For this example, I have downloaded Java JDK 21, the latest LTS version of Java under ~/Downloads folder.
Step 1: Identify the Home path for your Java JDK
In my case it is "/Users/code2care/Downloads/jdk-21.jdk/Contents/Home"
Step 2: Open .zshrc profile file using Nano/Vim Terminal Text editor
nano ~/.zshrc
Step 3: Add JAVA_HOME export details
export JAVA_HOME=/Users/code2care/Downloads/jdk-21.jdk/Contents/Home
Step 4: Add JAVA_HOME to PATH
export PATH=$JAVA_HOME/bin:$PATH
Save an exit the .zshrc file.
Step 5: Apply Changes
Run the below command in terminal to apply changes to Zsh shell.
source ~/.zshrc
Now if you do a java --version, you should see that you have the JAVA_HOME JDK being used.
[Macbook % java --version
openjdk 21 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to make div or text in html unselectable using CSS - CSS
- Skip Test Maven while creating package command - Java
- Fix Mac: A software update is required to connect to iPhone - MacOS
- How to activate and create SharePoint Spaces feature for mixed reality experience - 2D/3D images and web parts - SharePoint
- How to extract Java Jar/War/Ear files in Linux - Java
- Execute .bin and .run file Ubuntu Linux - Linux
- [fix] Docker: Alpine Linux - /bin/sh: bash: not found - Docker
- Bash Command to Do Nothing with Example - Bash