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.
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:
2. Find Java Installation Path
Determine where Java is installed by using the update-alternatives command, which manages installed versions of 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/environmentin a text editor with root privileges:
Add the following line (replace /usr/lib/jvm/java-11-openjdk-amd64 with your actual Java installation path):
- Using
/etc/profile.d/: Create a new script in this directory to set the environment variable.
Write the following script:
Make the script executable:
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:
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
| Action | Command/File | Purpose |
| Install Java | sudo apt install default-jdk | Install Java JDK using package manager |
| List Java Installations | update-alternatives --config java | Finds the installed Java versions and their paths |
| Edit environment variables | sudo nano /etc/environment | Edit system-wide environment variables |
| Create profile script | sudo nano /etc/profile.d/java.sh | Adds scripts to set variables on system profile load |
| Make script executable | sudo chmod +x /etc/profile.d/java.sh | Allows 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
- How to set java_home on Windows 7?
- How to set JFrame to appear centered, regardless of monitor resolution?
- How to set kafka-connect connector and task's JVM heap size?
- how to set kafka consumer concurrency using spring boot
- How to set multiple headers at once in Spring WebClient?
- How to set or change the default Java (JDK) version on macOS?
- How to set proxy on spring oauth2 OAuth2AccessToken request or How to override OAuth2AccessTokenSupport restTemplate variable?
- How to set rate limit for each user in Spring Boot?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.