Saturday, June 11, 2022

Java environment path in Windows, Linux and Mac - JAVA

We need to install Java and add JAVA_HOME in the Operating System user environment variable to work with software like Maven, Jenkins, React Native, Gradle, Tomcat and develop mobile apps with SDK. The path is required to be set for using tools such as javac, java, etc. If you have Java file outside the JDK/bin folder, it is necessary to set the path of JDK.

If you keep the Java source file inside the JDK/bin directory, the path is not required to be set because all the tools will be available in the current directory.




In this article, we going to share simple steps to add the Java JAVA_HOME  environment variable in Windows, Linux, and Mac. First of all, download JDK of Java from the official link for your Operating System.

There are two ways to set path of the Java in Window:

Temporary in Window

To set the temporary path of JDK, you need to follow the following steps:

  1. Open the command prompt 
  2. Copy the path of the JDK/bin directory 
  3. Write in command prompt: set path=copied_path

For Example:

set path=C:\Program Files\Java\jdk1.6.0_23\bin


Permanent in Window

For setting the permanent path of JDK, you need to follow these steps:

1. Go to My Computer properties.



2. Click on the advanced tab.

3. Click on environment variables.

4. Click on the new tab of the user variable.


5. Write the path in the variable name.


6. Copy the path of bin folder.

 
7. Paste path of bin folder in the variable value.


8. Click on OK Button.

 
9. Click on OK Button.


Now your permanent path is set. You can now execute any program of java from any drive.


Java Path In Mac

Here, we going to share Java installation and add JAVA_HOME in the Mac machine.

1. Open terminal to check Java install or not. Run java -version command to check it. 

If above window open. It's means Java not install in your Mac machine. So, click on OK button. It will open official Java download web page to download it. 


1Once, you download the pkg file of Java. You can find it in the download folder of the Mac machine:


You double click on it to install. After that, you have to enter your Mac machine password:


2. After that complete installation and you can delete (Click on Move to Trash button) pkg file from the system if the installation completed. Now you can check Java install or not with help of the above-given command (java -version). You will see the version of installed Java.
 

If you hit javac -version or echo $JAVA_HOME command in the terminal. It'll display nothing or Java path not set error. To add the java path, we have to find the java home path. You can do it with the help of $ /usr/libexec/java_home -v14 (-v14 is installed java version) command. 

3. To add java mac machine. We have to create .base_profile in the system. So, hit ls -al command in the terminal mac to check the .base_profile file available or not.


If it is not available. You can create it with the help of the following command:
  1. Start the Terminal.
  2. Type cd ~/ to go t your home folder.
  3. Type touch .base_profile to create your new file.
run again ls -al command. Now, the .base_profile will be create in the system as shown above.

4. After that, we have to open it to add the java path to this file. You can open with your favorite editor.
  • Type source .base_profile to reload and update it. 
5. You can open .base_profile two ways.
  1. Go to user folder and press  + Shift + . from the keyboard. You will see all hidden files of the system. So, simply find .base_profile and double click to open it

  2. You can open it vim ~/ .base_profile command.

6. Once you open it paste the following shown path of JAVA_HOME. You can ignore all other shown path if not required.

save it from file>> save button. Now if you hit java -version and javac -version and echo $JAVA_HOME command you will see all command working and displaying:


Tips for Mac machine path setup

If you still facing Java path setup issue. You can try following the given steps:
  1. Go to user folder and press  + Shift + . from the keyboard. You will see all hidden files of the system. So, simply find the .zshrc file and double-click to open it
paste above given JAVA_HOME in this file and save it as explain above. It'll will remove your issue to setup the java path.



Setting Java Path in Linux

Setting path in Linux OS is the same as setting the path in the Windows OS. But, here we use the export tool rather than set. Let's see how to set path in Linux OS:

export PATH=$PATH:/home/jdk1.6.01/bin/


Conclusion

I hope the approach shown in this post have been helpful to setup the environment variable of Java JDK and if you are facing any problem to implement it and you have any quires, please feel free to ask it from comment section below.



Share:

Get it on Google Play

Java environment path in Windows, Linux and Mac - JAVA

We need to install Java and add JAVA_HOME in the Operating System user environment variable to work with software like Maven, Jenkins, React...