1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. vim /etc/profile

    Prepend sudo if logged in as not-privileged user, ie. sudo vim

  3. Press 'i' to get in insert mode
  4. add:

    export JAVA_HOME="path that you found"
    
    export PATH=$JAVA_HOME/bin:$PATH
    
  5. logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell
Answer from That Dave Guy on Stack Overflow
๐ŸŒ
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 - Alternatively, type the following command to activate the new path settings immediately: $ source ~/.bash_profile OR $ . ~/.bash_profile Verify new settings: $ echo $JAVA_HOME $ echo $PATH Tip: Use the following command to find out exact path ...
๐ŸŒ
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
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.
๐ŸŒ
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 - Open ~/.bash_profile in any editor and add the following: ... Then save and close the file. Open a Terminal and run the source command to apply the changes: ... To set JAVA_HOME globally for all users, the steps are the same as for a single ...
๐ŸŒ
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 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. Persistent means that if you close your terminal or reboot your computer, you do not need to reset a value for the JAVA_HOME environment variable. The following example demonstrates using a text editor to enter commands for exporting JAVA_HOME in ~/.bashrc for a single user: > vi ~/.bash_profile export JAVA_HOME=/opt/jdk/11 export PATH="$JAVA_HOME/bin:$PATH"
๐ŸŒ
CodeJava
codejava.net โ€บ java-core โ€บ set-java-home-in-macos-linux
How to set JAVA_HOME in macOS and Linux permanently
December 30, 2021 - To setup JAVA_HOME with Bash shell, you create the Bash profile file (.bash_profile instead of .zshrc) in your user home directory, like this:
๐ŸŒ
Curious Programmer
curiousprogrammer.dev โ€บ blog โ€บ set-java-home-in-linux
Set $JAVA_HOME in Linux | Curious Programmer
Add the path to $PATH export $PATH=$PATH:$JAVA_HOME ยท Save the file. Source the file in your terminal eg. source ~/.zshrc or source ~/.bashrc
Find elsewhere
๐ŸŒ
Doyensys
doyensys.com โ€บ home โ€บ oracle fusion โ€บ setting java_home and the path in linux environment
Setting JAVA_HOME and the path in linux environment - Oracle Consulting Services | USA | 99% Customer Retention | Doyensys
August 2, 2021 - ~/.bashrc fi # User specific environment and start-up programs JAVA_HOME=/opt/jdk1.6.0_24 PATH=$JAVA_HOME/bin:$PATH:$HOME/bin export PATH JAVA_HOME unset USERNAME 3. Save the .bash_profile using the :wq command
๐ŸŒ
Medium
medium.com โ€บ @nirbhaysingh281 โ€บ set-java-home-environment-variable-permanently-f9d2c586609e
set JAVA_HOME environment variable Permanently - Nirbhay Singh - Medium
August 24, 2024 - To make this change persistent across sessions, you need to add the above commands to your shell configuration file (e.g., .bash_profile, .bashrc, or .zshrc), depending on the shell you're using. ... Now, the JAVA_HOME environment variable will be set every time you start a new shell session. After setting the JAVA_HOME, verify it by running: ... This should output /data/java11. ... Technophile, Assistant Vice President at CITI, talks about DevOps, Cloud, SRE, Linux and Programming.
๐ŸŒ
Daml
docs.daml.com โ€บ getting-started โ€บ path-variables.html
Set JAVA_HOME and PATH Variables โ€” Daml SDK 2.10.4 documentation
To set up JAVA_HOME and PATH variables ... make sure to change the java-version with the actual folder found on your computer: echo "export JAVA_HOME=/usr/lib/jvm/java-version" >> ~/.bash_profile......
๐ŸŒ
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 - On Windows, you'll set JAVA_HOME in Advanced System Settings > Environment Variables. On a Mac, you'll need to add the Java path to your .zshrc file in a Terminal. On Linux, you can add the Java path to your .bashrc.
๐ŸŒ
LinuxForDevices
linuxfordevices.com โ€บ home โ€บ how to set the java_home variable in linux?
How to Set the JAVA_HOME Variable in Linux? - LinuxForDevices
September 16, 2020 - The steps to set JAVA_HOME variable for all users are similar to setting it for a single user. You will need to log in as root or execute the commands using the sudo command. Changes in global configurations are done by editing the /etc/bashrc file.
๐ŸŒ
dartron
dartron.wordpress.com โ€บ 2014 โ€บ 05 โ€บ 02 โ€บ how-to-set-java_home-on-linux
How to set JAVA_HOME on Linux | dartron
May 2, 2014 - Login to the system Open a terminal window Open .bash_profile file for editing # vim ~/.bash_profile Set JAVA_HOME as following: export JAVA_HOME= export JAVA_HOME=/usr/bin/javaโ€ฆ
Top answer
1 of 3
2

From man bash:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

and

Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable. It will not do this if invoked as sh. The --norc option may be used to inhibit this behavior, and the --rcfile option may be used to force another file to be read, but rshd does not generally invoke the shell with those options or allow them to be specified.

In other words, ~/.profile and ~/.bashrc will normally both be read when you start bash, so you can put it in either.

You can also use the source luke in either file to load the file luke where you would define all your Javan variables. See help . or help source for details.

2 of 3
1

First, You will never add JAVA_HOME to your PATH. They serve different purposes as environment variables. The first, sets where your java binaries are installed so other java based programs can know. The second, is a set of places where bash will look for commands you type in terminal, like ls, cd etc.

Second, both the files will work, but you have to be careful when setting variables inside .bashrc, because you may overwrite the ones already configured in other files, as PATH. The most safe way of doing this is by adding ":$PATH" at the end of the path. It would be something like this (adds java binaries to your PATH to be able to call commands such as javac):

export PATH="/usr/java/jdk1.8.0_121/bin:$PATH"

To set JAVA_PATH, the most safe (and automatic... rsrs) way to do so is by adding the following code in the end of .bashrc:

export JAVA_HOME=$(update-alternatives --query javac | sed -n -e 's/Best: *\(.*\)\/bin\/javac/\1/p')

After you have done so, you will have do execute exec bash, or close and reopen the terminal window. To check if your variables are set, run on the terminal:

echo $JAVA_HOME
echo $PATH

You should be able to see the value inside the variables.

UPDATE: Remember that all these files like .bashrc are located in your home directory. Something like /home/<your name> or just by typing cd without any other arguments. Remember too, that files that start with a 'dot' . are not visible just by typing ls on terminal. if you want to see these files you have to ls -a

๐ŸŒ
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.
๐ŸŒ
thelinuxvault
thelinuxvault.net โ€บ blog โ€บ how-to-locate-and-set-java-home-directory-on-linux
How to Locate and Set JAVA_HOME Directory on Linux
... This file sets environment variables system-wide. ... Save and exit. The change takes effect after logging out and back in (or rebooting). Create a custom script in /etc/profile.d/ to set JAVA_HOME for all shells.