- Setting java user home
- How to set JAVA_HOME properly?
- Table of Contents
- 1. Set JAVA_HOME on Windows
- 1.1. Set JAVA_HOME on Windows 10 and 8
- 1.2. Windows 7
- 1.3. Verifying JAVA_HOME configuration
- 2. Set JAVA_HOME on Mac OS X
- 2.1. Mac OS X 10.5 or Newer — Single user
- 2.2. Mac OS X Older Versions
- 2.3. Global Setting
- 3. Set JAVA_HOME on Linux
- 3.1. Single User
- 3.2. Global Setting
- 4. Conclusion
- How to set JAVA_HOME environment in Linux, Unix and Windows? Example
- Setting JAVA_HOME on Windows environment:
- Setting JAVA_HOME on Unix or Linux
- JAVA_HOME vs java.home
- 5 comments :
Setting java user home
- A quick intro to the MACH architecture strategy While not particularly prescriptive, alignment with a MACH architecture strategy can help software teams ensure application .
- How to maintain polyglot persistence for microservices Managing microservice data may be difficult without polyglot persistence in place. Examine how the strategy works, its challenges.
- Top developer relations trends for building stronger teams Learn about enterprise trends for optimizing software engineering practices, including developer relations, API use, community .
- The basics of implementing an API testing framework With an increasing need for API testing, having an efficient test strategy is a big concern for testers. How can teams evaluate .
- The potential of ChatGPT for software testing ChatGPT can help software testers write tests and plan coverage. How can teams anticipate both AI’s future testing capabilities .
- Retail companies gain DORA metrics ROI from specialist tools DORA metrics and other measures of engineering efficiency are popping up in add-ons to existing DevOps tools. But third-party .
- How to create and manage Amazon EBS snapshots via AWS CLI EBS snapshots are an essential part of any data backup and recovery strategy in EC2-based deployments. Become familiar with how .
- Prices for cloud infrastructure soar 30% Tough macroeconomic conditions as well as high average selling prices for cloud computing and storage servers have forced .
- Deploy a low-latency app with AWS Local Zones in 5 steps Once you decide AWS Local Zones are right for your application, it’s time for deployment. Follow along in this step-by-step video.
- Multiple Adobe ColdFusion flaws exploited in the wild One of the Adobe ColdFusion flaws exploited in the wild, CVE-2023-38203, was a zero-day bug that security vendor Project .
- Ransomware case study: Recovery can be painful In ransomware attacks, backups can save the day and the data. Even so, recovery can still be expensive and painful, depending on .
- Supercloud security concerns foreshadow concept’s adoption Supercloud lets applications work together across multiple cloud environments, but organizations must pay particular attention to.
- AWS Control Tower aims to simplify multi-account management Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates .
- Break down the Amazon EKS pricing model There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service .
- Compare EKS vs. self-managed Kubernetes on AWS AWS users face a choice when deploying Kubernetes: run it themselves on EC2 or let Amazon do the heavy lifting with EKS. See .
How to set JAVA_HOME properly?
Before showing you how to set JAVA_HOME on all the operation systems, let’s explain what is an environment variable? And what is the use of JAVA_HOME environment variable?
Table of Contents
As a prerequisite you will need to have a installed JDK or JRE on your machine. See for details in article Install JDK aka Java SDK As you probably know, almost all java code has a variable that stores a value which is used many times in a program. An environment variable is like a final variable (in java) but for your environment. So, JAVA_HOME is a environment variable used to point to the directory where JRE or JDK is installed in your environment. Let’s go right to the point. So how to set JAVA_HOME on Windows, Mac os, and Linux?
1. Set JAVA_HOME on Windows
There are two ways to configure JAVA_HOME on windows depending on which version you have (windows 10, 8 or 7). In order to set JAVA_HOME environment variable, please follow below steps.
1.1. Set JAVA_HOME on Windows 10 and 8
To set JAVA_HOME on windows 10/8, you need to follow these steps below:
- Search for «advanced system settings» on windows Search
- Then, select «View advanced system settings» in the shown options
- Now you need to click on «Environment Variables» under the Advanced tab
- Click on «New (or User variables for single user)» in the System variables section
- In this step, you will have to set JAVA_HOME as the Variable name and in the Variable value you will have to give the path to JDK installation, then click OK
- To finish the configuration, click OK then Apply to save changes
1.2. Windows 7
Configuring the environment variable on windows 7 is little bit different. Below the steps.
- On the Start menu or on the desktop, right-click on «My computer» and then select «Properties«
- Click on «Environment Variables» under the Advanced tab
- Click on «New (or User variables for single user)» in the System variables section
- Now, set JAVA_HOME as the Variable name and as the Variable value, the path to JDK installation, then click OK
- Click OK then Apply to save and apply changes
1.3. Verifying JAVA_HOME configuration
To verify that you correctly set the JAVA_HOME environment variable, open Command prompt (windows + R) and check JAVA_HOME variable’s value:
If you have rightly configure JAVA_HOME, the result should be similar to the following:
2. Set JAVA_HOME on Mac OS X
Setting the JAVA_HOME environment variable on Mac OS is little bit different from configuring it on windows. To do so, all you need is to follow the steps described below.
2.1. Mac OS X 10.5 or Newer — Single user
Since OS X 10.5 was revealed, Apple added a command line tool (/usr/libexec/java_home) which dynamically finds for the current user, the top java version specified in Java Preferences.
Let’s start by opening ~/.bash_profile in any text editor and add:
export JAVA_HOME=$(/usr/libexec/java_home)
Then, save and close the file.
On the Terminal, run the following source command to apply all the changes.
At this point, you can check JAVA_HOME variable’s value using:
Below the expected result (jdk installation path)
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
2.2. Mac OS X Older Versions
Before OS X 10.5, you had to set the exact path to the JDK installation. If you have the concerned version of Mac Os, you need to to open ~/.bash_profile in any editor and add:
export JAVA_HOME=/path/to/java_installation
Then, save and close the file.
On the Terminal, run the following source command to apply all the changes.
At this point, you can check JAVA_HOME variable’s value using:
Below the expected result (jdk installation path)
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
2.3. Global Setting
Instead of setting JAVA_HOME for a single user, you can configure it globally by following the same steps as for single user except that you will have to use the file /etc/profile.
3. Set JAVA_HOME on Linux
On linux, there are two ways to set the JAVA_HOME environment variable. Below steps for configuring for both single user or globally.
3.1. Single User
To configure JAVA_HOME for a single user in Linux, you need to use /etc/profile or /etc/environment (system-wide setting or ~/.bashrc (user specific setting).
Now you need to open ~/.bashrc in your text editor using:
export JAVA_HOME=/path/to/java_installation
To apply changes, save and close the file.
Next step, you will have to run the source command to load the variable using the following:
To verify the JAVA_Home value, use:
And the result should be something similar to the following (the path of JDK installation):
3.2. Global Setting
I personally prefer to set the JAVA_HOME globally for all users using:
So you have to open /etc/environment in any text editor using:
JAVA_HOME=/path/to/java_installation
NB: /etc/environment is not a script. It represents a list of assignment which are read at the time of login.
To set the environment variable JAVA_HOME, you need to open /etc/profile and add in it:
export JAVA_HOME=/path/to/java_installation
Then run the source command in order to load the variable using:
Of course, you need to check JAVA_HOME variable’s value at the end of the configuration using:
And the result should be something similar to (the path of the JDK installation):
PS: Please note that these steps are also valid for Ubuntu OS. All that you need to verify, is to check where is java installed by:
Then, follow the steps described above.
4. Conclusion
To summarize, we’ve covered in today’s article, the steps that you need to follow to configure JAVA_HOME environment variable correctly in your environment (windows, mac os, and linux).
How to set JAVA_HOME environment in Linux, Unix and Windows? Example
JAVA_HOME is a system environment variable that represents the JDK installation directory. When you install JDK in your machine (Windows, Linux, or UNIX) it creates a home directory and puts all its binary (bin), library(lib), and othe r tools. In order to compile the java program «javac» tool should be in your PATH and in order to get that in PATH we use the JAVA_HOME environment variable. Many tools like ANT and web servers like tomcat use JAVA_HOME to find java binaries. In this article, we will see how to set the JAVA_HOME environment variable in the different operating systems including Windows (Windows 7, Vista, XP) and Linux (Unix).
Setting JAVA_HOME on Windows environment:
Windows environments (Windows 7, Vista, XP) are GUI-based operating systems and you can edit the PATH system variable from GUI. you need to define another variable called JAVA_HOME and set the Java installation directory as a value for the JAVA_HOME variable.
An important thing to note here is that on the Windows environment (Windows 7, Vista, XP), you have two sections user-defined variables and System Variables. a variable declared in one section is not visible in other sections.
So if you set JAVA_HOME on the user variable section and update PATH on System variable section, you will not be able to get the value of JAVA_HOME and it will be blank. So be careful with this.
And, If you are a Java beginner then I also recommend you go through these Java Beginner courses from Coursera and Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Setting JAVA_HOME on Unix or Linux
Setting JAVA_HOME on Unix or Linux environment is more easy and straightforward. you just need to export the JAVA_HOME variable and its value and then you can refer to its value in any shell script. here is an example of how to set JAVA_HOME on Unix and Linux:
Now Java is added to your system PATH by using the JAVA_HOME System variable. you can also check How to set PATH for Java in Linux or Unix for more details on steps and explanations.
JAVA_HOME vs java.home
I have seen Java programmer confusing over JAVA_HOME and java.home, where former represent JDK installation directory later represent JRE installation directory and you know Difference between JDK and JRE . JRE just contains java binaries and doesn’t contain other tools which come with JDK and are required for Java development. java.home is also a Java system property and you can access it on java code using System.getProperty(«java.home» ).
Also, all java system properties are exposed as ANT builtin property you can access java.home inside ANT as $
That’s all on how to set JAVA_HOME in Windows and Linux. We have also seen differences between JAVA_HOME and java.home system property. Let us know if you come across any other differences between them.
5 comments :
To set/export PATH for java-
export PATH= $:$/bin
One of the main advantage of using JAVA_HOME variable to point Java installation directory is that :
1) It’s easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.
2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.
3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.
4) JAVA_HOME is standard, which means other tools which needs Java e.g. Maven, Eclipse can refer this variable without having any knowledge of where Java is physically installed in your machine, which is obviously be different in different user’s machine. JAVA_HOME allows everybody to access Java in a common and standard way.
Let me know if this make sense.
Does Apache Maven also uses $JAVA_HOME to find out Java version to build a project? I am also confused with PATH and JAVA_HOME environment variable, if both contains location of JDK binary, which will take preference?
does setting JAVA_HOME variable is mandatory to run Java programs?