PATH and CLASSPATH

Pre-Requisites:

  • jdk1.6.0_11 ( works with previous versions of JDK and J2sdk)

 Installed location:

  • jdk1.6.0_11 -> C:\Program Files\Java\jdk1.6.0_11\

Difference between CLASSPATH and PATH

In PATH Environment variable you need to specify only .bat,.exe(Executable files that Operating System Uses).

In CLASSPATH environment you need to specify only .class files (i.e., jar, zip files – Inside jar, zip files you will find only java classes) i.e. you are helping Java Virtual Machine (JVM) to find Java class files

For running java application you need to set PATH alone. For versions above jdk 1.5, JAVA_HOME itself is enough. No need to set CLASSPATH.

How to set PATH

Shortcut for Environment Variables

Figure: 1


Click Window key + Pause/Break key

(OR)

Right click My Computer and click properties. You will get System Properties window. In that click Advanced->Environment variables. In User variables for “yoursystemname”. Click New

In Variable name and in Variable value type as follows.

(In my case I have installed java in C colon. In your case it might be in different colon. So change it accordingly. You might have installed jdk 1.5 then in that case navigate to C:\Program Files\Java\jdk1.5\bin and copy the things what you are having in address bar(Shortcut key is Alt+D) to Variable value.

Variable name: JAVA_HOME
Variable value: C:\Program Files\Java\jdk1.6.0_11

Variable name: PATH
Variable value: %PATH%;%JAVA_HOME%\bin

 

Don't know what is %PATH%,%JAVA_HOME%? then click

What is %PATH%,%CLASSPATH%,%JAVA_HOME%,%ANT_HOME%,%MAVEN_HOME%,%JBOSS_HOME%

Verify JDK:

Use the java -version command, as shown below, to verify the installed release:

D:\>java -version
java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)

The output you see may be different from what is shown above because the java -version command outputs vendor-specific information; however, if you don't see what looks like valid version information, then STOP! And don't proceed until you resolve that problem.

Note:

  1. Setting path doesn't require the computer to be restarted.
  2. After setting the path, close the command prompt and open a new command prompt and try typing java and javac to test.

Navigate to C:\Program Files\Java\jdk1.6.0_11\bin and see what it is inside that folder. You will find .exe files.

Inside that folder you will find javac.exe, java.exe, javap.exe, javah.exe, javadoc.exe appletviewer.exe……

Why we are setting PATH:

These .exe files can be accessed only from C:\Program Files\Java\jdk1.6.0_11\bin folder. Even you can run java programs without setting path. The thing is you need to type you program inside C:\Program Files\Java\jdk1.6.0_11\bin folder. Because java.exe, javac.exe…. are available only inside this folder.

We need to set path because we need to access .java.exe, javac.exe…..(all the files which are required to run java program) any where in your computer i.e., c colon, d colon, e colon… . If we set PATH then all the .exe files which are inside C:\Program Files\Java\jdk1.6.0_11\bin folder will be available throughout all the drives. So you can have java programs any where in your computer.

Why we are setting CLASSPATH:
 
For running java programs it not necessary to set CLASSPATH. It is necessary only when you use classes from third party .jar files. Here in this example I am taking the example of JDBC driver for MySQL.

mysql-connector-java-3.0.11-stable-bin.jar

If you extract the above jar file, you can find Driver.class, Connection interface, ResultSet interface... etc inside “com.mysql.jdbc” package.

Class.forName(“com.mysql.jdbc.Driver”);
Connection cn=DriverManager.getConnection("String","username","password”);

We need these classes and interfaces in all the drives. That is the reason behind setting CLASSPATH.

You can even create a java class file with a method and you can set that class file in CLASSPATH, So that you can access that method in all the drives.
 
D:\test\add\ClassPathTest.java

Create a folder test inside d: and inside test create one more folder with the name add.
And copy the below program and name the program as ClassPathTest.java

/**
 * ClassPathTest.java
 */
package add;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ClassPathTest {
	public int add(int a, int b) {
		return a + b;
	}
}

Compile the program using

D:\test\add>javac ClassPathTest.java
D:\test\add>

Right click My Computer and click properties. You will be getting System Properties window. In that click Advanced->Environment variables. In User variables for “yoursystemname”. Click New if no CLASSPATH doesn’t already exist. If CLASSPATH already exist click edit and append

Variable name:  CLASSPATH
Variable value: .;C:\apache-tomcat-6.0.14\lib\servlet-api.jar;D:\test

 

Type the below program any where in your computer for e.g.
In E:\Test.java I have my program

/*
 * Test.java
 */
import add.ClassPathTest;

/**
 * @author www.javaworkspace.com
 * 
 */
class Test {
	public static void main(String[] args) {
		ClassPathTest classPathTest = new ClassPathTest();
		System.out.println(classPathTest.add(5, 7));
	}
}

Run Test.java

E:\>javac Test.java

E:\>java Test
12

E:\>

You will be getting the output as 12.

Try copying the above program(Test.java) any where in you computer and try executing it.

How to make above program as .jar file and access  

Go to D:\test> and make jar file of you file ClassPathTest.
For creating jar files you need to give “jar cvf javaworkspace.jar .” At last I have given .(period) because all the files which is inside D:\test> will be made as jar file instead you can give as “jar cvf javaworkspace.jar .class

D:\>cd test

D:\test>jar cvf javaworkspace.jar .
added manifest
adding: add/(in = 0) (out= 0)(stored 0%)
adding: add/ClassPathTest.class(in = 260) (out= 194)(deflated 25%)
adding: add/ClassPathTest.java(in = 105) (out= 85)(deflated 19%)

D:\test>

Now copy that jar file(javaworkspace.jar) in any folder. In my case I am copying inside C:\Program Files\Java\jdk1.6.0_11\lib\

In Environment Variable

Remove the already existing D:\test

Enter Variable name and Variable value which is mentioned below.

Variable name: CLASSPATH
Variable value: .;C:\apache-tomcat-6.0.14\lib\servlet-api.jar;D:\test

Now change that as

Variable name: CLASSPATH
Variable value: .;C:\apache-tomcat-6.0.14\lib\servlet-api.jar;C:\Program Files\Java\jdk1.6.0_11\lib\javaworkspace.jar

 

That’s it. You can access ClassPathTest any where. What you have to do is copy javaworkspace.jar in a pendrive and copy that in your friends system and set classpath for that and type only Test.java file and run. You will be getting output. Like this some one has written some classes and interface and packed in to a jar file called servlet-api.jar, mysql-connector-java-3.0.11-stable-bin.jar… etc.., and given to you. So you are setting classpath for the jar file. And by creating object for the class which is present inside the .jar file you are accessing the methods.

For queries mail us at: javaworkspace@gmail.com