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
🌐
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 - At this point, you can run echo $JAVA_HOME to see the location of Java on your system. ... Include your email address to get a message when this question is answered. Submit ... In Linux, you can set JAVA_HOME for all users by editing the global ...
🌐
Java67
java67.com › 2016 › 06 › how-to-set-javahome-and-path-in-linux.html
How to set JAVA_HOME and PATH in Linux? Example | Java67
export JAVA_HOME=/usr/sun/jdk/v1.6.0_16-64bit/ export PATH = $JAVA_HOME/bin:$PATH in second line, shouldn't be space befor & after = ReplyDelete ... Load more... Feel free to comment, ask questions if you have any doubt. ... Difference between table scan, index scan, and ind... How to find Nth Highest Salary in MySQL and SQL Se... ... How to use recursive grep command in Linux and UNI...
🌐
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 - Here we’re going to manipulate the PATH, so these are the detailed instructions on how to do it. To set JAVA_HOME in Linux for a single user, we can use /etc/profile or /etc/environment (preferred for system-wide setting) or ~/.bashrc ...
🌐
It's FOSS
itsfoss.com › set-java-home-ubuntu
How to Set JAVA_HOME Variable in Ubuntu Linux Correctly
November 17, 2023 - To set JAVA_HOME variable ‘permanently’, you should add it to the bashrc file in your home directory. You can use the Nano editor for editing files in the Linux terminal. If you do not want that and take a simple copy-paste approach, use the following commands:
🌐
Curious Programmer
curiousprogrammer.dev › blog › set-java-home-in-linux
Set $JAVA_HOME in Linux | Curious Programmer
Add this line to export the path the the corresponding directory export JAVA_HOME=/usr/bin/java which can be determined using which java if you are unsure. Add the path to $PATH export $PATH=$PATH:$JAVA_HOME · Save the file.
🌐
LinuxConfig
linuxconfig.org › home › how to locate and set java home directory on linux
How to locate and set JAVA home directory on Linux
June 6, 2023 - We will set the Java home directory on our Linux system by configuring the environment variable for JAVA_HOME. Open your user’s ~/.bashrc file in nano or your preferred text editor. ... Be sure to replace the directory above with your actual Java home directory (where all of your Java JDK and JRE files reside). Afterwards, save changes and exit the file. Then, run the following command in terminal for the changes to take effect:
Find elsewhere
🌐
nixCraft
cyberciti.biz › nixcraft › howto › bash shell › how to set java_home / path variables under linux bash profile
How to Set JAVA_HOME / PATH variables Under Linux Bash Profile - nixCraft
May 25, 2012 - Login to your account and open .bash_profile file $ vi ~/.bash_profile Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows: export JAVA_HOME=/usr/ja...
🌐
Delft Stack
delftstack.com › home › howto › java › linux set java_home
How to Set Java_home in Linux | Delft Stack
February 26, 2025 - After sourcing the file, you can confirm that the variable is set by running echo $JAVA_HOME in the terminal. If you want to set JAVA_HOME globally for all users on your Linux system, you can add the export command to the /etc/environment file.
🌐
Ask Ubuntu
askubuntu.com › questions › 1362508 › update-info-how-to-currently-set-java-home
command line - Update info - how to currently set Java_home? - Ask Ubuntu
September 8, 2021 - I don't exactly why, but after several restarts (not after just one) the path got set instantly instead of me having to manually source it. ... export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar export GRADLE_HOME=/opt/gradle-8.1 PATH=$PATH:$GRADLE_HOME/bin export MAVEN_HOME=/opt/apache-maven-3.9.1 PATH=$PATH:$MAVEN_HOME/bin
🌐
Scaler
scaler.com › home › topics › how to set java_home?
How to set JAVA_HOME? - Scaler Topics
May 4, 2023 - Example-1 ... Once you have saved ... user system in Linux, in order to set JAVA_HOME use /etc/profile or /etc/environment (preferred for system-wide setting) or ~/.bashrc ....
🌐
javathinking
javathinking.com › blog › how-to-set-java-home-in-linux-for-all-users
How to Set JAVA_HOME in Linux for All Users: Step-by-Step Guide (Root, Non-Root & Java Path) — javathinking.com
It’s a simple, distribution-agnostic method. Open /etc/environment with a text editor (e.g., nano or vim). You’ll need sudo privileges: ... Add the JAVA_HOME line at the end of the file, replacing <your-java-path> with the path from Step 1:
🌐
Planetary Data System
pds.nasa.gov › datastandards › training › documents › Finding and Setting JAVA HOME.pdf pdf
Finding and Setting JAVA HOME
system property settings, including the value of java.home, which is what we need. To see your Java ... If the answer is 1.7 or later, then this command will dump several dozen lines of property settings to ... Note that java dumps this listing to stderr rather than stdout, so the usual pipe through more will not · have the desired effect. You are going to have to either scroll or, on linux-based systems, redirect
🌐
YouTube
youtube.com › watch
Easily Set JAVA_HOME on Linux in Just 2 Minutes! (Step-by-Step Guide) | 2025 - YouTube
#Linux #JAVA_HOME #SetEnvironmentVariable #JavaOnLinux #LinuxTutorial #JavaInstallation #UbuntuJava #LinuxJavaSetup #ConfigureJAVA_HOME #JavaEnvironmentSetup...
Published   January 23, 2025
🌐
VITUX
vitux.com › how-to-setup-java_home-path-in-ubuntu
How to Set JAVA_HOME Path in Ubuntu 20.04 and Ubuntu 22.04 – VITUX
January 21, 2024 - You can find your Java installation path by executing update-alternatives --config java in the terminal. This command lists all installed Java versions and their paths. ... To set JAVA_HOME, open your terminal and type export JAVA_HOME=/path/to/java.
🌐
Opsgenie Docs
docs.opsgenie.com › docs › setting-java_home
Setting JAVA_HOME - Opsgenie Docs
Open a New Command Prompt · Execute : "SET JAVA_HOME" Output should be like JAVA_HOME=path · Install for Single User : Check if JAVA_HOME is already set , Open Console · Execute : echo $JAVA_HOME · If output is a path , then your JAVA_HOME ...
🌐
Red Hat
access.redhat.com › solutions › 1445833
How to set the JAVA_HOME environment variable? - Red Hat Customer Portal
The echo $JAVA_HOME command-line must return the complete path directory to java executable. For detailed instructions, please see JBOSS Enterprise Application Platform Installation Guide: Install OpenJDK on Red Hat Enterprise Linux
🌐
Medium
computingpost.medium.com › set-java-home-on-centos-rhel-fedora-afa5842e46ef
Set JAVA_HOME on CentOS / RHEL / Fedora | by ComputingPost | Medium
October 21, 2022 - You can set JAVA_HOME in .bash_profile, .bashrc file or for all Global users in /etc/profile or as bash function inside /etc/profile.d/ directory. Add below line to any of bash dotfiles mentioned above.