Thursday, 5 January 2012

Java Classpath & How to Set Classpath in Java


In Java the compiler and JVM uses the classpath to locate classes when they are referenced by other Java code.Compiler and JVM identifies the location of these classes using the -classpath option on the javac command line or by using the CLASSPATH environment variable.

Class paths can be set to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:
  • For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
  • For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
  • For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
Classpath entries that are neither a directory nor an archive (.zip or .jar file) are ignored.

Packages on the classpath

Java classes are organized into packages which are mapped to directories in the file system. But, unlike the file system, whenever you specify a package name, you specify the whole package name never part of it. For example, the package name for java.awt.Button is always specified as java.awt.


Suppose we have a package called org.mypackage containing the classes:

Main (main class)
SupportClass

In Microsoft Windows
D:\myprogram\
      |
      ---> org\  
            |
            ---> mypackage\
                     |
                     ---> Main.class       
                     ---> SupportClass.class   
                         
In Linux
/home/user/myprogram/
            |
            ---> org/  
                  |
                  ---> mypackage/
                           |
                           ---> Main.class       
                           ---> SupportClass.class   


When we invoke Java, we specify the name of the application to run: org.mypackage.Main. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we use the following command:

In Windows: java -classpath D:\myprogram org.mypackage.Main
In Linux: java -classpath /home/user/myprogram org.mypackage.Main

where:

-classpath D:\myprogram sets the path to the packages used in the program (on Linux, -classpath /home/user/myprogram)
org.mypackage.Main is the name of the main class

Note that if we ran Java in D:\myprogram\ (on Linux, /home/user/myprogram/) then we would not need to specify the classpath since Java implicitly looks in the current working directory for files containing classes.

Note that the entire package name is specified in the command. It is not possible, for example, to set the class path so it contains D:\myprogram\org\ and use the command java mypackage.Main. The class would not be found

Setting the path through an environment variable

The environment variable named CLASSPATH may be alternatively used to set the classpath. For the above example, we could also use on Windows:
set CLASSPATH=D:\myprogram
java org.mypackage.Main
Jars on the classpath
Java compiler and run-time can search for classes not only in separate files, but also in `JAR' archives. A JAR file can maintain its own directory structure, and Java follows exactly the same rules as for searching in ordinary directories. Specifically, `directory name = package name'. Because a JAR is itself a directory, to include a JAR file in the class search path, the path must reference the JAR itself, not the directory that contains the JAR. This is a very common error. Suppose I have a JAR jarclasses.jar in directory /jarclasses. The Java compiler look for classes in this jar, we need to specify:
javac -classpath /jarclasses/jarclasses.jar ...
and not merely the directory jarclasses.
 Now, suppose the program uses a supporting library enclosed in a Jar file called supportLib.jar, physically in the directory D:\myprogram\lib\.
The corresponding physical file structure is :
D:\myprogram\
      |
      ---> lib\
            |
            ---> supportLib.jar
      |
      ---> org\
            |
            --> mypackage\
                       |
                       ---> Main.class
                       ---> SupportClass.class
                     

We should use the following command-line option:

java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.Main
or alternatively:
set CLASSPATH=D:\myprogram;D:\myprogram\lib\supportLib.jar
java org.mypackage.Main

Multiple Specifications

To find class files in the directory C:\java\MyClasses as well as classes in
C:\java\OtherClasses,you would set the class path to: C:> java -classpath C:\java
\MyClasses;C:\java\OtherClasses Note that the two paths are separated by a semicolon(In Linux use : instead of ;).
Specification order
The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the example above, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses.Only if it doesn't find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.

No comments:

Post a Comment