In this tutorial you will learn How to set path in java in Windows as well as Linux/Unix. This tutorial explained with screenshots so that you can easily set the Java paths without any confusion.

Before we begin setting the Path, it is better to know 3 mostly used terms for a Java application to be run.

PATH: Path is an environment variable which is used by the operating system to find the executable. In our case for Java setting PATH means telling Operating system to where to find the Java install or JDK location. As we discussed in earlier post HERE JDK contains tools such as Java compiler (javac) which is used to convert Java files in to Class files.

CLASSPATH: ClassPath is also an environment variable which is used by the Java Compiler or JVM to find and locate the user defined classes or packages.

JAVA_HOME: Java Home is an environment variable set to be used by the tools like Tomcat, Gradle etc.. Java Home is not mandatory to run a Java application. Java_Home is useful especially if you have multiple Java versions installed and want to point your tools like tomcat to a specific java version out of many installed. In general we define JAVA_HOME as root of the java installed directory.

Now, let us learn how to set up the Java path

How to set path in Java?

Now, before we start Compile and Run the java programs we need to download and install the JDK and configure some of the system environment variables. The JRE and JDK can be downloaded from Oracle website directly for Windows and Linux. The official link for the website is:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

To download the JDK 8 and JRE 8 for Windows and Linux from the official website is:

https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html#A1096936

After you download the JDK you need to install it. The process of installation differ from one operating system to another.

How to install JDK on Windows?

It is easy to install JDK on Windows, just simply double click the executable file which you have downloaded and then follow the instructions and it will by default download the JRE file also. You should be having the folders like below in your Windows machine.

JDK Install directory in Windows

 

How to set the path for Windows?

For compiling and running the java programs on Windows it is important to set the PATH environment variable on your computer, so that you can invoke the javac and java from any directory. Below are the steps which needs to be followed to set the path:

  • Click start, then go to Settings and then to Control Panel.
  • Then click the System and Security.
  • Then select the System
  • Then select the Advanced tab and click on Environment Variables
  • Now locate the PATH environment variable in the User Variables or the System Variables panes. The value of the PATH is basically a series of directories which is separated by the semicolons. Now you have to add the full path to the bin directory in your Java installation directory till the end of the existing value of the Path. The directory will look like this: C:\Program Files\Java\jdk1.8.0_<version>\bin
  • Now, click Ok or Apply.

You can test the setup by opening Command Prompt and typing Java or Javac commands. If they say Java or Javac cannot be found then issue with updating variables, so please follow above stps carefully and give correct path to JDK directory.

How to install JDK on Linux?

In Linux, JDK is available in two installation formats. They are:

  • First is, RPM it supports the package management system like Red Hat and SuSE.
  • And second is Self extracting package where a compressed file containing packages to be installed.

If we are using RPM, then we have to follow these steps:

  • It becomes the root by using the su command.
  • Then extract the downloaded file.
  • Then change the directory to that location where the downloaded file is located and then type:
    chmod a+x rpmFile
    Where rpmFile is the RPM file.
  • Then run the RPM file like this:
    ./rpmFile

If we are using a self extracting binary installation, then we have to follow these steps:

  • Firstly, extract the downloaded file.
  • Then use the chmod command to give the file, execute permissions, like this:
    chmod a+x binFile
    where the binFile is the downloaded bin file for our platform.
  • Now change the directory to the location where we want the files to be installed
  • After changing the directory run the self extracting binary and then execute the downloaded file with the path prepended to it, for example, if the file is in the current directory then prepend it with “./”
    ./binFile

How to set PATH in Linux?

For setting the PATH environment variable in this operating system depends on the shell we use. For the C shell add the following at the end of your ~/.cshrc file:

set path= (path/to/jdk/bin $path)

where path/to/jdk/bin is the bin directory in your JDK installation directory.

And for the Bourne again shell adds ~/.bashrc or ~/.bash_profile at the end of your file.

Export PATH= /path/to/jdk/bin: $PATH

In this, path/to/jdk/bin is the bin directory in your JDK installation directory.

How to test the installation?

To know that you have installed the JDK properly and correctly is by typing javac on the command line from any directory on your machine. If you can see the instructions on how to correctly run Java, then you have successfully installed it. And if you can only run Java from the bin directory of the JDK installation directory, then your PATH environment variable is not configured properly.

 

Let us know If you are successful with your setup or still facing issues.