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:

  1. Open the /etc/environment file in a text editor. You will need to use sudo to edit this file, since it requires root privileges. For example:

    sudo nano /etc/environment

    This will open the /etc/environment file in the nano text editor.

  2. Add a line to the file that sets the JAVA_HOME variable. The line should look like this:

    JAVA_HOME="/path/to/java"

    Replace /path/to/java with the path to the Java installation that you want to set as the default.

  3. Save the file and exit the text editor.

  4. 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_HOME

This will print the value of the JAVA_HOME variable.