Monday, 20 August 2012



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

Click Window key + Pause/Break key
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 toC:\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

Verify JDK:
Use the java -version command, as shown below, to verify the installed release:
  1. D:\>java -version  
  2. java version "1.6.0_11"  
  3. Java(TM) SE Runtime Environment (build 1.6.0_11-b03)  
  4. 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.

  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.exeappletviewer.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 which is inside C:\Program Files\Java\jdk1.6.0_11\binfolder is available throughout all drives. So you can have java programs any where in your computer.
Why we are setting CLASSPATH:

For running java programs no need to set CLASSPATH. Here in this example I am taking the example of JDBC driver for MySQL.
If you extract the above jar file, you can find Driver.class, Connection interface, ResultSet interface... etc inside “com.mysql.jdbc” package.
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.

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
  1. /** 
  2.  * 
  3.  */  
  4. package add;  
  6. /** 
  7.  * @author 
  8.  *  
  9.  */  
  10. public class ClassPathTest {  
  11.     public int add(int a, int b) {  
  12.         return a + b;  
  13.     }  
  14. }  
Compile the program using
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:\ I have my program
  1. /* 
  2.  * 
  3.  */  
  4. import add.ClassPathTest;  
  6. /** 
  7.  * @author 
  8.  *  
  9.  */  
  10. class Test {  
  11.     public static void main(String[] args) {  
  12.         ClassPathTest classPathTest = new ClassPathTest();  
  13.         System.out.println(classPathTest.add(57));  
  14.     }  
  15. }  
E:\>java Test
You will be getting the output as 12.
Try copying the above program( 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/ = 105) (out= 85)(deflated 19%)
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 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.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...