You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.

Open /etc/environment in any text editor like nano or gedit and add the following line:

JAVA_HOME="/usr/lib/jvm/open-jdk"

(java path could be different)

Use source to load the variables, by running this command:

source /etc/environment

Then check the variable, by running this command:

echo $JAVA_HOME

Update

Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)

source /etc/environment
Answer from Manula Waidyanatha on askubuntu.com
🌐
Oracle
docs.oracle.com › cd › E19182-01 › 820-7851 › inst_cli_jdk_javahome_t
Installing the JDK Software and Setting JAVA_HOME (Using the GlassFish ESB Installation CLI)
Set JAVA_HOME. Korn and bash shells: export JAVA_HOME=jdk-install-dir · export PATH=$JAVA_HOME/bin:$PATH · Bourne shell: JAVA_HOME=jdk-install-dir · export JAVA_HOME · PATH=$JAVA_HOME/bin:$PATH · export PATH · C shell: setenv JAVA_HOME jdk-install-dir ·
🌐
Baeldung
baeldung.com › home › java › set java_home variable in windows, mac os x, and linux
Set JAVA_HOME on Windows 7, 8, 10, Mac OS X, Linux | Baeldung
March 26, 2025 - To set JAVA_HOME in Linux for all users, we can use /etc/profile or /etc/environment (preferred). Open /etc/environment in any text editor and add the following: ... Please note that /etc/environment isn’t a script, but a list of assignment expressions (that’s why export isn’t used).
🌐
Medium
zendesk.engineering › setting-java-home-on-mac-os-acd44e3a6027
Setting JAVA_HOME on Mac OS. This was originally posted in 2013 on… | by Barry Carroll | Zendesk Engineering
August 20, 2018 - Naturally if you don’t want to use the latest and greatest you could specify that you want to use the 64-bit JDK 1.7 like this: export JAVA_HOME=`/usr/libexec/java_home -d 64 -v "1.7*"`
Find elsewhere
🌐
Atlassian
confluence.atlassian.com › spaces › CROWD › pages › 61604243 › Setting+JAVA_HOME
Setting JAVA_HOME | Crowd Data Center 7.2 | Atlassian Documentation
Add export PATH=$JAVA_HOME/bin:$PATH ... window · vi /etc/profile (replace vi with your favorite text editor) Add export JAVA_HOME=/path/to/java/home/dir on its own line at the end of the file ·...
🌐
TheServerSide
theserverside.com › feature › How-to-set-JAVA_HOME-in-Windows-and-echo-the-result
How to set JAVA_HOME in Windows | TheServerSide
After you set JAVA_HOME in Windows, it's a good idea to verify the change has persisted. The easiest way to do this is to echo JAVA_HOME in a command prompt or a Bash shell. To echo JAVA_HOME in a DOS prompt, simply bookend the variable with ...
🌐
Medium
medium.com › @zorozeri › setting-up-java-home-5abae0118bfe
Setting up JAVA_HOME. Guideline for Windows, MacOS, and… | by Ahmad Azeri Chandra Bhuana | Medium
November 13, 2024 - 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
🌐
Chef
discourse.chef.io › chef infra (archive)
How to set JAVA_HOME AND PATH in linux and windows while writing chef cookbook - Chef Infra (archive) - Chef Questions
July 3, 2019 - Cookbook for Linux: execute "export JAVA_HOME" do command "export JAVA_HOME=#{jdk_java_home}" end execute "export" do command "export PATH=#{jdk_path}" end Path for linux: JAVA_HOME = /home/oracleweblogic/wls/jdk…
🌐
Mkyong
mkyong.com › home › java › how to set $java_home environment variable on macos
How to Set $JAVA_HOME environment variable on macOS - Mkyong.com
January 19, 2021 - For zsh shell, export $JAVA_HOME at `~/.zshenv` or `~/.zshrc`; For bash shell, export $JAVA_HOME at `~/.bash_profile` or `~/.bashrc`
🌐
Planetary Data System
pds.nasa.gov › datastandards › training › documents › Finding and Setting JAVA HOME.pdf pdf
Finding and Setting JAVA HOME
% JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre · % export JAVA_HOME · C-shell-type shells can do this in a single step: % setenv JAVA_HOME /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre · and there's an alternate syntax that works just as well: % set JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre ·
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to set java_home on windows, linux, & mac
How to Set JAVA_HOME on Windows, Linux, & Mac
March 5, 2025 - Set the JAVA_HOME path. Now that you have the location of Java, you can add the path to your .bashrc file (if you're using the bash shell), .zshrc (for the zsh shell), or the equivalent for your shell.[3] X Research source Run both of the following commands, replacing the path with the path to your JDK or JRE installation, and "bashrc" with your shell's login script: echo "export JAVA_HOME=/usr/lib/jvm/java-23-openjdk-arm64" >> ~/.bashrc
🌐
Red Hat
access.redhat.com › solutions › 1445833
How to set the JAVA_HOME environment variable? - Red Hat Customer Portal
For the bash shell, this file is /home/username/.bashrc. At the bottom of the file, type the following line, replacing the hypothetical path with the actual path to use on the operating system: export JAVA_HOME="/path/to/java/home".
🌐
Mkyong
mkyong.com › home › java › how to set java_home on windows (step-by-step guide)
How to Set JAVA_HOME on Windows (Step-by-Step Guide) - Mkyong.com
July 29, 2025 - C:\Users\mkyon>java -version openjdk version "21.0.8" 2025-07-15 LTS OpenJDK Runtime Environment Temurin-21.0.8+9 (build 21.0.8+9-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.8+9 (build 21.0.8+9-LTS, mixed mode, sharing) C:\Users\mkyon>javac -version javac 21.0.8 C:\> echo %JAVA_HOME% C:\Program Files\Java\jdk-21
🌐
Baeldung
baeldung.com › home › java › core java › java_home vs path environment variables
JAVA_HOME vs PATH Environment Variables | Baeldung
January 8, 2024 - In this tutorial, we’ll explore the key difference between using JAVA_HOME and PATH environment variables. Though most Java programs need both these variables to compile and run successfully, each serves a different purpose.