Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Saturday, 20 July 2013

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


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 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.

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.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.
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
  1. /** 
  2.  * ClassPathTest.java 
  3.  */  
  4. package add;  
  5.   
  6. /** 
  7.  * @author www.javaworkspace.com 
  8.  *  
  9.  */  
  10. public class ClassPathTest {  
  11.     public int add(int a, int b) {  
  12.         return a + b;  
  13.     }  
  14. }  
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
  1. /* 
  2.  * Test.java 
  3.  */  
  4. import add.ClassPathTest;  
  5.   
  6. /** 
  7.  * @author www.javaworkspace.com 
  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. }  
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.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...