How to set JAVA_HOME in Linux for all users
To set the JAVA_HOME environment variable in Linux for all users, you will need to add a line to the /etc/environment file.
Here's how you can do this:
Open the
/etc/environmentfile in a text editor. You will need to usesudoto edit this file, since it requires root privileges. For example:sudo nano /etc/environmentThis will open the
/etc/environmentfile in thenanotext editor.Add a line to the file that sets the
JAVA_HOMEvariable. The line should look like this:JAVA_HOME="/path/to/java"Replace
/path/to/javawith the path to the Java installation that you want to set as the default.Save the file and exit the text editor.
Reload the environment variables by logging out and logging back in, or by running the following command:
source /etc/environment
After you have set the JAVA_HOME variable in the /etc/environment file, it will be available to all users on the system. You can verify that the variable has been set correctly by running the following command:
echo $JAVA_HOMEThis will print the value of the JAVA_HOME variable.