How to Set Java_home in Linux
- Method 1: Setting JAVA_HOME Temporarily in the Terminal
- Method 2: Setting JAVA_HOME Permanently for a User
- Method 3: Setting JAVA_HOME Globally for All Users
- Conclusion
- FAQ

Setting the JAVA_HOME environment variable is crucial for Java developers working in Linux. It helps the system locate the Java installation, enabling various applications and tools to function correctly. Whether you are installing a new Java version or configuring an existing one, knowing how to set JAVA_HOME can save you time and frustration.
In this article, we will explore several methods to set JAVA_HOME in Linux, complete with code examples and detailed explanations. By the end, you’ll have a solid understanding of how to manage this essential environment variable effectively.
Method 1: Setting JAVA_HOME Temporarily in the Terminal
If you need to set JAVA_HOME for a single session, you can do so directly in the terminal. This method is useful for testing or running a specific application without making permanent changes to your system.
Here’s how to do it:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
After running this command, you can verify that JAVA_HOME has been set correctly by using the following command:
echo $JAVA_HOME
Output:
/usr/lib/jvm/java-11-openjdk-amd64
In this example, we used the export
command to set the JAVA_HOME variable temporarily. This means it will only last as long as your terminal session is open. Once you close the terminal, the variable will be lost. This method is particularly handy for testing different Java versions or configurations without altering your system’s permanent settings.
Method 2: Setting JAVA_HOME Permanently for a User
If you want to set JAVA_HOME permanently for a specific user, you can add the export command to the user’s profile file. This way, the variable will be set automatically each time the user logs in.
Here’s how to do it:
- Open the
.bashrc
file in your home directory with a text editor:
nano ~/.bashrc
- Add the following line at the end of the file:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
- Save the file and exit the editor. To apply the changes, run:
source ~/.bashrc
By adding the export command to the .bashrc
file, you ensure that JAVA_HOME is set every time you log in. This method is excellent for users who work with Java frequently and need a consistent environment. Remember to replace the path with your specific Java installation path. After sourcing the file, you can confirm that the variable is set by running echo $JAVA_HOME
in the terminal.
Method 3: Setting JAVA_HOME Globally for All Users
If you want to set JAVA_HOME globally for all users on your Linux system, you can add the export command to the /etc/environment
file. This method is useful for system administrators who need to configure Java for multiple users.
Here’s how to do it:
- Open the
/etc/environment
file with superuser privileges:
sudo nano /etc/environment
- Add the following line at the end of the file:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
- Save the file and exit the editor. To apply the changes, restart your system or log out and back in.
In this method, we edited the /etc/environment
file, which is read by the system at startup. The JAVA_HOME variable will be available to all users, making it an ideal choice for server environments or multi-user systems. After making these changes, you can verify that JAVA_HOME is set globally by running echo $JAVA_HOME
from any user’s terminal.
Conclusion
Setting the JAVA_HOME environment variable in Linux is essential for Java developers and system administrators alike. Whether you need a temporary solution for a single terminal session or a permanent setup for all users, the methods outlined in this article provide clear and effective ways to manage this crucial environment variable. By understanding how to set JAVA_HOME, you can streamline your Java development process and avoid common pitfalls related to Java installations.
FAQ
-
How can I find the path of my Java installation?
You can find the path by running the commandreadlink -f $(which java)
in the terminal. -
Is it necessary to set JAVA_HOME for Java applications to work?
While not always mandatory, many Java applications require JAVA_HOME to locate the Java installation, making it a good practice to set it. -
Can I set multiple JAVA_HOME variables for different Java versions?
Yes, you can set different JAVA_HOME variables temporarily in the terminal for different sessions or scripts. -
What should I do if I forget to set JAVA_HOME and encounter errors?
You can temporarily set JAVA_HOME in your terminal session and troubleshoot the issue. Make sure to set it permanently if you need it frequently. -
Will changes to JAVA_HOME affect running applications?
Changes to JAVA_HOME will only affect applications that are started after the change has been made. Existing processes will not be affected.
Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.
LinkedIn