Setting Environment Variables JAVA_HOME, PATH and CLASSPATH in Java in Windows

After Java installation our second step is to set environment variables PATH, CLASSPATH and JAVA_HOME for compiling and running of Java applications. Let’s discuss them one by one.

PATH (For Windows Users)

When we launch any program from command line prompt, operating system use PATH environment variable to locate executable programs. In other words, PATH maintains a list of directories for searching executable programs such as “.exe”, “.bat” or “.com”.

For Example: When you run command javac Hello.java or java Hello, Windows searches the program in the current working directory and all the directories listed in the PATH for Java Compiler “javac.exe” and Java Runtime “java.exe”.

In case of failure it shows error given below:

PATH maintains a set of directories. The directories are separated by semi-colon ‘;’.

For Java applications, PATH must include JDK’s “bin” directory (e.g., “c:\Program Files\java\jdk1.6.0_xx\bin”), which contains JDK programs such as Java Compiler “javac.exe” and Java Runtime “java.exe”.

In Windows, the current working directory ‘.’ is automatically included in the PATH, as the first entry. In other words, the current working directory is searched first, before searching the other paths specified in PATH, in the order specified.

For Windows users (in Vista / Windows 7) you should set the PATH permanently to include JDK’s “bin” directory via “Control Panel” > “System and Security” > “System” > “Advanced system settings”. Switch to “Advanced” tab > “Environment variables”. Choose “System Variables” (for all users) or “User Variables” (for this login user only). Select variable “PATH” and choose “Edit” (for modifying an existing variable). In variable “Value” append your JDK’s “bin” directory (e.g., “c:\Program Files\java\jdk1.6.0_xx\bin”), followed by a semi-colon ‘;’, in front of all the existing PATH entries. DO NOT remove any entry; otherwise, some programs may not run.

CLASSPATH (For Windows Users)

This variable maintains path of java class files. Java files can be packaged into JAR. CLASSPATH accepts directories and JAR files. If class file is not found in directories or jar-files defined in CLASSPATH it gives “NoClassDefFoundError”.

The directories and jar-files are separated by semi-colon ‘;’. An example of CLASSPATH setting, which includes the current working directory (denoted as dot ‘.’) and Java Servlets API jar-file from Tomcat (e.g., d:\tomcat\lib\servlet.jar) is as follow:

prompt> SET CLASSPATH=.;d:\tomcat\lib\servlet.jar

Since JDK 1.3, if no CLASSPATH is set explicitly, the default is set to the current working directory ‘.’. However, if you explicitly set your CLASSPATH, you have to include the current directory ‘.’ explicitly. Otherwise, the current directory will not be searched. A common problem during JDK installation is CLASSPATH was set (probably by another application) without including the current directory. You will receive “NoClassDefFoundError” when you run your Hello-world program.

For a beginner, no explicit CLASSPATH setting is required. The default CLASSPATH setting of currently directory is sufficient. Remove all CLASSPATH setting if there is any. However, if you have to set CLASSPATH, make sure that you include the current directory ‘.’.

For windows users (in Vista / Windows 7), you can set the CLASSPATH permanently via “Control Panel” > “System” > “Advanced system settings”. Switch to “Advanced” tab > “Environment variables” and choose “System Variables” (for all users) or “User Variables” (for this login user only):

1) (RECOMMENDED) Delete CLASSPATH from both System and User variables if exists.

2) To modify the existing CLASSPATH, select variable “CLASSPATH” and Choose “Edit”. In variable “Value”, provide the directories and jar-files, separated by semi-colon ‘;’. Make sure that the current directory ‘.’ is included as the first entry.

3) To create CLASSPATH, choose “New”. In variable “Name”, enter “CLASSPATH” and in variable “Value”, provide the directories and jar-files, separated by semi-colon ‘;’. Make sure that the current directory ‘.’ is included as the first entry.

JAVA_HOME (For Windows Users)

JRE (Java Runtime) is needed for running Java programs. JDK (Java Development Kit) is needed for writing and running Java programs. JDK includes JRE plus development tools such as Java compiler.

Set JAVA_HOME to your JDK installation directory (e.g., “c:\Program Files\java\jdk1.6.0_xx”). JAVA_HOME is needed for running Tomcat and many other Java applications. JAVA_HOME is the directory you install your JDK.

For windows users (in Vista / Windows 7), you can set the JAVA_HOME permanently via “Control Panel” > “System” > “Advanced system settings”. Switch to “Advanced” tab > “Environment variables” and choose “System Variables” (for all users) or “User Variables” (for this login user only). To create JAVA_HOME, choose “New”. In variable “Name”, enter ” JAVA_HOME” and in variable “Value”, provide the JDK installation directory.

You can optionally set JRE_HOME to the JRE base directory (e.g., “c:\Program Files\java\jre1.6.0_xx”)

After setting all above environment variable you can compile and run your Java application freely.

About The Author

Scroll to Top