How to set JAVA_HOME?
How to Set Java Home for Windows?
We traditionally use the variable JAVA_HOME to set the installation path for JAVA on our local systems. In this article, we will learn how to set Java Home for Windows, Linux, Mac and Ubuntu systems. JAVA_HOME is used to set the installation address for Java on one's local computer. This specifies the location where JAVA is to be installed. There are 3 examples of setting Java Home in Windows. These examples are:
- allowing the Java installer to do it;
- using the environment variable editor; and
- using the command line.
Example-1: Java Installer A JAVA_HOME configuration tool can configure tha path of Java installation on your Windows system if you perform a custom install. Following the install, you can either choose
- add to PATH; or
- set JAVA_HOME variable.
Once you have made the selection, the installer would automatically install Java in the designated 'Home' location.
Example-2: Using the Environment Variable Editor to Set Java Home
- Open the Windows--> System--> Properties--> Control Panel applet on any version of Windows.
- Select Advanced System settings.
- Click on the Environment Variables button.
- Select the New button under System Variables.
- Set JAVA_HOME as the environment variable name.
- Set the location of the JDK installation as the value of the environment variable.
- Click OK and close the JAVA_HOME environment variable editor.
Once you open a new command window, scripts that search for JAVA_HOME will execute successfully.
Example-3: Using the Command Line Interface Using the command line interface, the setx command can be used to configure Java Home.
The output for this would be:
This is how easily one could java home using the command line interface.
How to set java home in Mac?
Apple has introduced a command line tool (/usr/libexec/java_home) that automatically looks for the most suitable Java version specified in Java Preferences for a given user. Example-1
- Open Bash Script and type:
- Once you have saved the file, open a terminal window and run the given source command to apply the changes:
- Check the value of the JAVA_HOME variable:
The result for this code would be the path of the JDK installation.
How to Set Java Home in Linux?
Example-1: For a Single User
-
For a single user system in Linux, in order to set JAVA_HOME use /etc/profile or /etc/environment (preferred for system-wide setting) or ~/.bashrc .
-
Open ~/.bashrc in a text editor of your choice and append the following:
- Save the file and execute the given source command to load the variable:
- Now check the value of the JAVA_HOME variable by executing the following command:
The output would be the path where JDK would be installed.
Example-2: Global Setting
-
For all users, use /etc/profile or /etc/environment (more preferable) to set JAVA_HOME in Linux.
-
Open /etc/environment in any text editor of your choice and append the given code:
Note that /etc/environment is not a script but a list of assignment expressions. It is read only while logging in.
- To set JAVA_HOME using /etc/profile, append this export statement to the path:
- Execute the source command to initiate the variable:
- Now check the value of the JAVA_HOME variable:
The output for this would be the path where JDK would be installed.
How to Set Java Home in Ubuntu?
Example To set Java Home in Ubuntu:
- Configure the environment variables by editing the system path file by using /etc/profile.
- Set the value of the JAVA_HOME variable as the required path:
- Configure the Path
- Export the required variables:
- Type java -version to ensure that Java is accessible.
Learn to Setup JAVA_HOME & the IDE on MAC OS
If you would like to learn how to set up JAVA_HOME on the IDE in MacOS, click here
Conclusion
- We traditionally use the variable JAVA_HOME to set the installation path for JAVA on our local systems.
- There are 3 examples of setting Java Home in Windows. These examples are:
- allowing the Java installer to do it;
- using the environment variable editor; and
- using the command line
- Java home can be set on linux in the private setting as well as in the global setting.