How to Set JAVA HOME for all users on Linux

To set the JAVA_HOME environment variable for all users on Linux, you can modify the system-wide profile file. Here’s how you can do it:

  • Open a terminal and login as the root user or use sudo to gain root privileges.
  • Determine the Java installation directory:
    • Identify the directory where your Java JDK is installed. This will be the value you set for JAVA_HOME.
    • Note that the exact location may vary depending on the Java distribution and version you have installed.
    • Common paths include /usr/lib/jvm/java-<version> or /usr/java/jdk-<version>.
  • Edit the system-wide profile file:
    • Open the profile file using a text editor. The location of the profile file may differ based on your Linux distribution, but commonly used files include /etc/profile, /etc/bashrc, or /etc/environment.
    • Choose one of the above files based on your system and preferences.
  • Add the JAVA_HOME environment variable:
    • Insert the following line at the end of the profile file:
    • export JAVA_HOME=/path/to/java/directory
    • Replace /path/to/java/directory with the actual path to your Java installation directory determined in step 2.
  • Save the changes and exit the text editor.
  • Load the updated profile:
    • To apply the changes immediately, you can either reboot the system or run the following command to reload the profile file:
    • source /etc/profile
  • Verify the JAVA_HOME setting:
    • Run the following command to verify that the JAVA_HOME environment variable is set correctly:
    • echo $JAVA_HOME
    • It should display the path you set in step 4.

Now, the JAVA_HOME environment variable will be available system-wide for all users on your Linux system. This ensures that any Java-related tools or applications can locate the Java installation directory consistently.

Note that modifying system-wide configuration files requires administrative privileges. Ensure you have the necessary permissions or consult your system administrator if needed.