CLASSPATH

Classpath


  • ClassPath is a keyword. It is used to specifying third party .class files to another class file (or) java file.
  • It is mainly used for java environment system only, but not for the Operating System.
  • Path variable is 100% used for operating System for specifying java commands to operating system.
  • We can set ClassPath in three ways.
    • 1.) Single command wise
      2.) Single command prompt wise
      3.) Entire system wise
    javapadho
    Set classpath Folder Structure

Example 1:

Calculator.java

 
						
                    package com.sdj.calci;
                    public class Calculator 
                    {
                       public static int add(int val1,int val2)
                       {
                          return (val1+val2);
                       }
                    } 
                        

Accounts.java

 
						
                    package com.sdj.account;
                    import com.sdj.calci.Calculator;
                    class Accounts 
                    {
                       public static void main(String[] args) 
                       {
                          int sum=Calculator.add(10,20);
                          System.out.println("Sum of 10,20 is:"+sum);
                       }
                    } 
                        
    E:/lab/1stbatch/dev1/src>javac –d ../classes com/sdj/calci/Calculator.java
    E:/lab/1stbatch/dev2/src>javac –d ../classes com/sdj/account/Accounts.java

    Here we will get No Class Definition Found Error i.e. com.sdj.Calculator.java
    • If we want to compile Accounts.java first we should have to finish the compilation of Calculator.java
    • Compiler will looks the Calculator.java file inside the E:/lab/1stbatch/dev2/src but the Calculator.java is available under E:/lab/1stbatch/dev1/src so we get No Class Definition Found Error.
    • In order to avoid this,we should specify where the class is available.

    1.) Single command wise :


    • E:/lab/1stbatch/dev2/src>javac -d ../classes –classpath ../../dev1/classes com/sdj/account/Accounts.java
    • Here we get an Error i.e. can’t find Accounts.java inside dev1/classes .Because it looks under the dev1/src which is not available.
    • E:/lab/1stbatch/dev2/src>javac -d ../classes –classpath ../../dev1/classes;. com/sdj/account/Accounts.java
    • In the above command compiler will go to Calculator.class and then go the current location i.e. E:/lab/1stbatch/dev2/src

    2.) Single command prompt wise :


    • Single command prompt wise makes .class files available untill and unless we close the command prompt.
    • Once we close the command prompt then that .class files will not be available whenever we open the new command prompt.
    • Syntax :
    •     Set classpath=”destination of .class file location”

      Output

        E:/lab/1stbatch/dev2/src>set classpath=../../dev1/classes;.
        E:/lab/1stbatch/dev2/src>javac –d ../classes com/sdj/account/Accounts.java
        E:/lab/1stbatch/dev2/classes>java com.sdj.account.Accounts
        Sum of 10,20 is:30

    3.) Entire System Wise :


    • Right click on computer then it will following .
    • javapadho
      Click on the properties
    • Click on the properties then it will show the following wizard.
    • javapadho
      Click on the Advance System settings
    • Click on the Advance System settings then it will show the following wizard then click on Advanced.
    • javapadho
      By clicking on Advanced
    • By clicking on Advance then it will show following wizard.
    • javapadho
      Environment Varialbles
      • Click on new option for entering new classpath entry, then it will show following wizard
      • Now enter variable name and variable value as shown in below.
      • We can enter N no.of variable values followed by semicolon.
      javapadho
      New User Variables
    • Click on ok button for completion of classpath entry.
    • javapadho
      User varialbles add
    • Finally click on ok button to exit from the environment variables.
    • javapadho
      ok button to exit from the environment variables