Sitemap
Ahmad Azeri Chandra Bhuana

Ahmad Azeri Chandra Bhuana

Software Quality Assurance

Setting up JAVA_HOME

6 min readOct 23, 2024

--

Press enter or click to view image in full size
Image Credit to Geralt

Download and Install JDK is easy. Just download, double click, and wait. It’ll finish quickly. But putting it into PATH environment?

Different OS have different treatment. Here I will try to show their differences on Windows (11), MacOS, and Linux (Ubuntu — 24.10).

Windows (11)

After JDK is installed, check the java version using cmd :

java --version
Press enter or click to view image in full size

Alright, it is installed successfully. Now let’s setup the JAVA_HOME.

Open Environment Variable, and under “User variables for [your user]”, click button New :

Press enter or click to view image in full size

Put :

  • Variable Name = JAVA_HOME
  • Variable Value = [folder location where JDK is installed], for example C:\Program Files\Java\jdk-23
Press enter or click to view image in full size

Click OK. Now, you should see JAVA_HOME is added.

Now, click on Variable Path (maybe below JAVA_HOME) :

Press enter or click to view image in full size

Click New and write :

%JAVA_HOME%
Press enter or click to view image in full size

Then click OK (on Edit environment variable), click OK again (on Environment variables), and OK (System properties).

Now, verify it by open new cmd, and run :

echo %JAVA_HOME%
Press enter or click to view image in full size

Congratulations!

JAVA_HOME is exist on your Windows Path environment now.

Note : you can also modify the Path on “System variables” instead of “User variables for [your user]”. This way, your JAVA_HOME will be used by all of the User on your Computer.

Press enter or click to view image in full size

MacOS

After JDK is installed, check the java version using cmd :

java --version
Press enter or click to view image in full size

Alright, it is installed successfully. Now let’s setup the JAVA_HOME.

But first, you need to know what kind of shell you used. If you never change anything related with your shell, it should be either bash or zsh. You can see the type from the terminal’s title, or to be sure, run command :

echo $SHELL
Press enter or click to view image in full size

If you use bash, you need to update file .bashrc. But to be sure, you can also update file .bash_profile and .profile as well.

echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.bashrc
echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.bash_profile
echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.profile

If you use zsh, you need to update file .zshrc. But to be sure, you can also update file .zprofile and .zlogin as well.

echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.zshrc
echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.zprofile
echo 'export "JAVA_HOME=$(/usr/libexec/java_home)"' >> ~/.zlogin

For my example, I use zsh, so I run the command for zsh :

Press enter or click to view image in full size

Or, you can directly open those files on your “/Users/{username}/” folder, but since they are hidden, you need to hit “Shift+Command+.” combination to make them visible.

Press enter or click to view image in full size

Now, verify the path changes, by open new terminal, and run :

echo $JAVA_HOME
Press enter or click to view image in full size

Congratulations!

Get Ahmad Azeri Chandra Bhuana’s stories in your inbox

Join Medium for free to get updates from this writer.

JAVA_HOME is exist on your MacOS Path file now.

Linux (Ubuntu)

Note : For this one, I only able to try it on Ubuntu (24.10). If you use other Linux distro, you can try the guideline I put here, but if it is failed, then you must google it yourself.

After JDK is installed… no, no, no. I’ll step back a bit here, special case for Ubuntu.

Installing JDK on Ubuntu, is a bit different compared to Windows or MacOS. To equalize the topic with Windows and MacOS above, I will still use Oracle Website for this guideline, to install OracleJDK, instead of OpenJDK. You can google by yourself if you prefer installing OpenJDK (many sources cited it is easier).

You can install OracleJDK using .deb file from Oracle Website, but if you only have ARM Machine, then you might stuck with .tar.gz or .rpm file.

I will show example of installing .rpm file here.

First, we need to install Alien Package. This package may be useful for you in the future for installing other .rpm files on Ubuntu.

sudo apt update
sudo apt install alien -y

Now, cd into folder where you download the .rpm file, and install it using command :

sudo alien -i [package_name].rpm

It might take a while.

Press enter or click to view image in full size
Press enter or click to view image in full size

Now, after JDK is installed, you might want to run command :

java --version
Press enter or click to view image in full size

Hehe, gotcha 🙈

Well, yeah, just installing it will not make java command executable on Ubuntu. That’s why I take a step back before, remember?

If you execute the commands shown above (sudo apt install default-jre and so on), you’ll install OpenJDK (or JRE on this case). It might be easier. But since our topic here is about OracleJDK, I won’t use those commands.

So, why the java command still not recognized even though we already succeeded installing it?

Because, installing using OracleJDK, means we choose the “manual installation” way. Yes, Ubuntu suggest to use OpenJDK because OpenJDK is maintained as part of the Ubuntu archive (that’s why they can be installed through apt command). Installing JDK using other method will not be supported “officially”.

So, what should we do?

Well, directly setup our JAVA_HOME of course!

Similar with process on MacOS, we need to know what kind of shell we use on our Ubuntu. Run these command to check it :

echo $SHELL

Chance is, if you never change anything shell-related on your Ubuntu, you might still using bash, as per seen on my screenshot above. So, let’s update several file related bash shell to make sure everything’s works.

Update .bashrc, .bash_profile, and .profile file :

echo 'export JAVA_HOME="/usr/lib/jvm/jdk-23.0.1-oracle-aarch64"' >> ~/.bashrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bashrc
echo 'export JAVA_HOME="/usr/lib/jvm/jdk-23.0.1-oracle-aarch64"' >> ~/.bash_profile
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bash_profile
echo 'export JAVA_HOME="/usr/lib/jvm/jdk-23.0.1-oracle-aarch64"' >> ~/.profile
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.profile
Press enter or click to view image in full size

If you use zsh shell, update .zshrc, .zprofile, and .zlogin instead.

echo 'export JAVA_HOME="/usr/lib/jvm/jdk-23.0.1-oracle-aarch64"' >> ~/.zshrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zshrc
echo 'export JAVA_HOME="/usr/lib/jvm/jdk-23.0.1-oracle-aarch64"' >> ~/.zprofile
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zprofile
echo 'export JAVA_HOME="/usr/lib/jvm/jdk-23.0.1-oracle-aarch64"' >> ~/.zlogin
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zlogin

And remember, this folder path /usr/lib/jvm/{oracle-jdk-version} might be different on your Local Machine, depends on what Oracle JDK version you installed. Check it on your related folder :

Press enter or click to view image in full size

Or, rather than execute command above, you can also directly open those files on your “/home/{username}/” folder, and edit them. Since they are hidden, you need to hit “Ctrl+H” combination to make them visible.

Press enter or click to view image in full size

Alright, now close and re-open your terminal, and run the magic word for java :

java --version
Press enter or click to view image in full size

Yessssssss, it works, right?

And then, verify that the path changes successfully :

echo $JAVA_HOME
Press enter or click to view image in full size

Congratulations!

JAVA_HOME is exist on your Ubuntu Path file now.

Understanding Path Environment might be needed not just for setting up JAVA_HOME, but other tools as well (python? pyenv? ANDROID_HOME? It’s just a few example). You can use this article as reference for setting up other Path Environment just as how you setup JAVA_HOME.

--

--