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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ setting-environment-java
Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME - GeeksforGeeks
May 9, 2025 - Go inside System Variables section, select the path variable. Right-click on it and choose the option Edit. In the Edit environment window, click New. ... Then add the path to the JDK's bin folder (e.g., C:\Program Files\Java\jdk-23\bin).
๐ŸŒ
Medium
medium.com โ€บ @noah24.kim โ€บ how-to-set-java-environment-variable-in-ubuntu-4244311211ad
How to set Java environment variable in Ubuntu | by Noah T. K | Medium
April 23, 2023 - Now, add this Java path to the PATH variable in order to use Java. export PATH=$PATH:$JAVA_HOME/bin echo $PATH Then you will see the PATH variable which added Java path. ... Now you are done with setting up Java environment variable.
๐ŸŒ
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
The new environment variable will not be added to an existing shell, you need to open a new shell window. Also, if you use bash shell then adding into ~/.bash_profile makes sense, if you use csh (C shell) then add JAVA_HOME into ~/.csh_profile, and similarly for ksh (K shell) add JAVA_HOME into ~/.ksh_profile. They are hidden files in your home directory in Linux, that's why they had prefix dot (.) in their name.
๐ŸŒ
Helical Insight
helicalinsight.com โ€บ home โ€บ blog โ€บ configure java_home/jre_home environment variables for linux
Configure JAVA_HOME/JRE_HOME Environment Variables For Linux
February 16, 2026 - Add the following line to your ~/.bashrc, ~/.zshrc, or ~/.profile file, depending on your shell: export JAVA_HOME=/usr/lib/jvm/java-25-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH ...
๐ŸŒ
NTU Singapore
www3.ntu.edu.sg โ€บ home โ€บ ehchua โ€บ programming โ€บ howto โ€บ Environment_Variables.html
Environment Variables for Java Applications - PATH, CLASSPATH, JAVA_HOME
For example, to add a directory to the PATH environment variable, add the following line at the end of "~/.bashrc" (or "~/.bash_profile", or "~/.profile"), where ~ denotes the home directory of the current user, or "/etc/profile" for ALL users. // Append a directory in front of the existing PATH export PATH=/usr/local/mysql/bin:$PATH ยท (For Java) You can set the CLASSPATH environment variables by adding the following line.
๐ŸŒ
Oracle
java.com โ€บ en โ€บ download โ€บ help โ€บ path.html
How do I set or change the PATH system variable?
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK. Reopen Command prompt window, and run your java code. To run a different version of Java, either specify the full path, or use ...
๐ŸŒ
2DayGeek
2daygeek.com โ€บ home โ€บ how to setup java environment variables on linux
How to setup JAVA Environment Variable in Linux | 2DayGeek
October 25, 2017 - But I noticed that in the above section regarding โ€œ3) Permanent : Setup JAVA Environment Variable to single userโ€ your paths to Java are in /opt Just a thought โ€“ Please let me know if you think I should change my setup m/ ... i want cancle this work . can i reset this configure? please give me a mail. my mail address is [ [email protected] ] ... These are the easiest and most comprehensive directions to follow! I will come to 2daygeek for all of my Linux questions from now on.
Find elsewhere
Top answer
1 of 4
1

Using 12.04, placing the information in /etc/environment worked for me:

tku@buster:~$ cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
[...]
LC_MEASUREMENT="de_DE.UTF-8"
JAVA_HOME=/home/tku/work/jdk1.7.0_05
2 of 4
1

You wanna make sure your Java is the default:

Set Default Java Version

Use the alternatives command to set the default java version.

~ ~โ†“โ†“$โ†“โ†“ sudo alternatives --config java

Select Java:

If your system has multiple Java versions, the above command will list all Java versions.

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
 + 1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.12.0.7-4.fc35.x86_64/bin/java)
   2           java-latest-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.rolling.fc35.x86_64/bin/java)
*  3           /usr/java/jdk-17.0.1/bin/java

Enter to keep the current selection[+], or type selection number: whateverisyours


Verify Java Version

Check the java version using the following command.

~ ~โ†“โ†“$โ†“โ†“ java --version

output feks.:

java 18.0.2 2022-07-19
Java(TM) SE Runtime Environment (build 18.0.2+9-61)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing)

Then, add the required variables.

To set the environment variables for a particular user, place the below variables in the .bash_profile file in the home directory or in the /etc/environment file for system wide settings.

~ ~โ†“โ†“$โ†“โ†“ sudo nano /etc/environment

and your information there is current version of Java:

export PATH=$PATH:/usr/java/jdk-18.0.2/bin/
export JAVA_HOME=/usr/java/jdk-18.0.2/  

if your using JRE then it will look something like this:

export PATH=$PATH:/usr/java/jre*****/bin/
export JAVA_HOME:/usr/java/jre*****/

Test it by executing:

~ ~โ†“โ†“$โ†“โ†“ echo $JAVA_HOME
~ ~โ†“โ†“$โ†“โ†“ echo $PATH
~ ~โ†“โ†“$โ†“โ†“ java --version
๐ŸŒ
Cleverence
cleverence.com โ€บ articles โ€บ oracle-documentation โ€บ setting-an-environment-variable-for-java-5824
How to Set Java Environment Variables (JAVA_HOME, PATH) on Windows, macOS, and Linux
May 3, 2026 - Use jEnv or SDKMAN! on macOS/Linux, or Gradle/Maven toolchains to declare per-project versions. On Linux servers, update-alternatives can set a system default; for services, set JAVA_HOME in the unit file.
๐ŸŒ
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 - I installed jre-7 beat onto /opt .I appended the PATH of bin of java to /etc/environment variable and it works. ... java_vm throws out error โ€œjava_vm process: could not find Java VM symbolsโ€ ,Any Idea how to set PLUGIN_HOME variable for a jre 7 install(manually installed from jar archive) ... Hi , set JAVA_HOME and PATH variables for every user under my Linux system(ubuntu 10.04) open profile $sudo vi /etc/profile; insert into last line: #set java environment JAVA_HOME=/usr/lib/jvm/java-6-sun // I am using jdk1.6.0.20 CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar PATH=$PATH:$JAVA_HOME/bin export JAVA_HOME export CLASSPATH export PATH export GDK_NATIVE_WINDOWS=true #this is for setting eclipse #env GDK_NATIVE_WINDOWS=1 #me too!
๐ŸŒ
Red Hat
docs.redhat.com โ€บ en โ€บ documentation โ€บ red_hat_build_of_openjdk โ€บ 21 โ€บ html โ€บ configuring_red_hat_build_of_openjdk_21_on_rhel โ€บ configuring-javahome-environment-variable-on-rhel
Chapter 5. Configuring the JAVA_HOME environment variable on RHEL | Configuring Red Hat build of OpenJDK 21 on RHEL | Red Hat build of OpenJDK | 21 | Red Hat Documentation
You know where you installed Red Hat build of OpenJDK on your system. For example, /opt/jdk/11. ... Set the value of JAVA_HOME. ... Verify that JAVA_HOME is set correctly. ... You can make the value of JAVA_HOME persistent by exporting the environment variable in ~/.bashrc for single users or /etc/bashrc for system-wide settings.
๐ŸŒ
Oracle
docs.oracle.com โ€บ cd โ€บ F51302_01 โ€บ English โ€บ Installing โ€บ p6_eppm_install_config โ€บ 89522.htm
Setting an Environment Variable for Java
October 1, 2025 - In the Variable value field, insert %JAVA_HOME%\bin; in front of all the existing directories. Do not delete any existing entries; otherwise, some existing applications may not run. ... Exit the Control Panel. Setting the Environment Variables with Unix or Linux
๐ŸŒ
Twilio
twilio.com โ€บ en-us โ€บ blog โ€บ developers โ€บ community โ€บ working-with-environment-variables-in-java
Setting Environment Variables in Java | Twilio
June 11, 2025 - If the environment variable that your code needs is one of the ones defined by your operating system, such as %COMPUTERNAME% by Microsoft Windows or $HOME on Linux, macOS, and FreeBSD, then no. Otherwise, yes, they need to be set either by your code, or by the process that launches your Java ...
๐ŸŒ
Cleverence
cleverence.com โ€บ articles โ€บ oracle-documentation โ€บ java-environment-variables-and-path-5821
Java Environment Variables: Configure JAVA_HOME, PATH, and CLASSPATH on Windows, macOS, and Linux
March 13, 2026 - IDEs often maintain their own JDK settings independent of the OS environment. Check the IDEโ€™s project SDK/JRE and language level. Align them with your project and toolchain configs. If discrepancy persists, print versions in both contexts (terminal and IDE run configuration) to see what each resolves. -Where should I define variables for Linux services? Prefer explicit definitions in systemd unit files (Environment=JAVA_HOME=...) or referenced EnvironmentFile entries.