Linux
JAVA_HOME
Java
System Configuration
User Settings

How to set JAVA_HOME in Linux for all users

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Setting the JAVA_HOME environment variable on a Linux system is crucial for developers and applications that rely on Java. This variable tells the operating system where Java is installed and allows software to correctly utilize the Java Runtime Environment (JRE) or Java Development Kit (JDK). When you need to configure this for all users, system-wide settings are implemented.

Understanding JAVA_HOME

The JAVA_HOME environment variable points to the directory where Java is installed. On Linux, typical installation directories for Java are /usr/lib/jvm/java-<version>-openjdk-amd64 or similar paths depending on the version and type of Java (OpenJDK, Oracle JDK, etc.) installed.

Steps to Set JAVA_HOME for All Users

1. Install Java

Before setting the JAVA_HOME variable, ensure that Java is installed. You can install Java using your distribution’s package manager. For example, in Ubuntu, you could use:

bash
sudo apt update
sudo apt install default-jdk

2. Find Java Installation Path

Determine where Java is installed by using the update-alternatives command, which manages installed versions of Java:

bash
update-alternatives --config java

This command lists all installed Java versions and their paths. Note the path of the Java version you wish to set as the default.

3. Setting JAVA_HOME Globally

To set JAVA_HOME for all users, you need to edit a system-wide configuration file like /etc/environment or scripts in /etc/profile.d/.

  • Using /etc/environment: This file is ideal for setting environment variables globally.
    Open /etc/environment in a text editor with root privileges:
bash
   sudo nano /etc/environment

Add the following line (replace /usr/lib/jvm/java-11-openjdk-amd64 with your actual Java installation path):

plain
   JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
  • Using /etc/profile.d/: Create a new script in this directory to set the environment variable.
bash
   sudo nano /etc/profile.d/java.sh

Write the following script:

bash
   export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
   export PATH=$PATH:$JAVA_HOME/bin

Make the script executable:

bash
   sudo chmod +x /etc/profile.d/java.sh

4. Verify the Configuration

After setting the JAVA_HOME, log out and log back in, or reboot the machine to apply the changes. To check if JAVA_HOME is set, use:

bash
echo $JAVA_HOME

This should output the path to your Java installation.

5. Additional Configuration for Applications

Some Java-based applications or development tools might require additional environment variables or specific configurations linked to JAVA_HOME. Ensure to consult the documentation specific to those tools and adjust configurations as necessary.

Table: Key Commands and Files for Setting JAVA_HOME in Linux

ActionCommand/FilePurpose
Install Javasudo apt install default-jdkInstall Java JDK using package manager
List Java Installationsupdate-alternatives --config javaFinds the installed Java versions and their paths
Edit environment variablessudo nano /etc/environmentEdit system-wide environment variables
Create profile scriptsudo nano /etc/profile.d/java.shAdds scripts to set variables on system profile load
Make script executablesudo chmod +x /etc/profile.d/java.shAllows the script to run when a user logs in

Conclusion

Setting the JAVA_HOME variable system-wide ensures that all users and applications on the system have a consistent environment for running and developing Java applications. It's important for system administrators and developers to ensure the path is correctly configured to prevent runtime errors related to missing or incorrect Java environment settings. A correctly set JAVA_HOME prioritizes functionality across various Java-based applications and tools, fostering a stable development or production environment.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.