Wednesday, March 26, 2014

EXPANDING JAVA CONCEPT Part 3 - Package And Classpath

In EXPANDING JAVA CONCEPT series,I have explained about the Packages Strategy . Here I will discuss how to create package and how it relates with the classpath.

Creating Package  
If you are using any IDE like Eclipse,its very easy step and to create package in eclipse you can follow my post First Java Program In Eclipse . If you want to write in Text editor then you must create directory same as specified in the package statement of java source file.
example : if you write a class like below, then you must create directory in/technoscience/first in you local directory. like d:\javaproject\src\in\technoscience\first\Abc.java

package in.technoscience.first;

public class Abc {
 public Abc(){
  
 }
 public static void main(String[] args) {
  System.out.println("hello java");
 }

}
Consider following points while using package:
  • While writing Java program, you must place every class you define into a package, and give each package a unique name.
  • You place a class into a package by including a package declaration at the top of the source file. 
  • A package declaration is just the keyword package followed by the package name and a semicolon. 
  • The package declaration must appear in the source file before any class or interface declaration, and each source file can contain only one package declaration.
  • Example as i have written in above code : package in.technoscience.first
Now consider some examples:
Theses examples will illustrate the concept of classpath , initially try to follow this practicals and at the end you will come to know exactly in which situation classpath plays a big role.
Example 1 
We shall write a class called Circle in package in.technoscience. It is a good practice to store the source codes and the classes in separate directories, as it facilitate the distribution of classes without the source codes.
Suppose that we write and save the source as d:\myJavaProject\src\in\technoscience\Circle.java, and want the compiled class save as in d:\myJavaProject\classes\in\technoscience\Circle.class. let us write the source as follow :
package in.technoscience;
public class Circle {
private double radius;
public Circle(double radius) {
   this.radius = radius;
}
public double getRadius() {
   return radius;
}
public void setRadius(double radius) {
   this.radius = radius;
}
}

To compile the source using JDK, we need to use the -d option to specify the package base directory of the compiled class d:\myJavaProject\classes as follows (-d defaulted to the current directory):
 javac -d d:\myJavaProject\classes d:\myJavaProject\src\in\technoscience\Circle.java 

The compiled class will be kept in d:\myJavaProject\classes\in\technoscience\Circle.class. Directory "in.technoscience" will be created automatically
Let's write a test program to use this Circle class. Suppose that TestCircle.java is saved in d:\myOtherProject.

import in.technoscience.Circle;
public class TestCircle {
public static void main(String[] args) {
   Circle c = new Circle(1.23);
   System.out.println(c.getRadius());
}
}
If we compile TestCircle.java from the directory d:\myOtherProject, we will get a error message, as the compiler cannot find the in.technoscience.Circle class.

d:> cd \myOtherProject

d:\myOtherProject> javac TestCircle.java
TestCircle.java:1: package in.technoscience does not exist
import in.technoscience.Circle;
              ^
We need to use the -cp (or -classpath) option to specify the base directory of the package in.technoscience, in order to locate in.technoscience.Circle. As CLASSPATH variable is a Java way to tell the possible locations of user classes or jar files for a Java application.
d:\myOtherProject> javac -cp d:\myJavaProject\classes  TestCircle.java

while running the TestCircle, we again get a error, as JRE cannot find the in.technoscience.Circle.
d:\myOtherProject> java TestCircle
Exception in thread "main" java.lang.NoClassDefFoundError: in/technoscience/Circle
 
Let include the base directory of the package in.technoscience in the classpath (to locate in.technoscience.Circle):
d:\myOtherProject> java -cp d:\myJavaProject\classes TestCircle 
Exception in thread "main" java.lang.NoClassDefFoundError: TestCircle 

But now, the JRE can't even find the TestCircle class, which is located in the current directory. This is because if CLASSPATH is not explicitly set, it defaulted to the current directory. However, if CLASSPATH is explicitly set, it does not include the current directory unless the current directory is included. Hence, we need to include current directory (denoted as '.') in the CLASSPATH, together with the base directory of package in.technoscience, separated by ';', as follows:

d:\myOtherProject> java -cp .;d:\myJavaProject\classes TestCircle 1.23
Example 2 Suppose that the TestCircle class in Example 1 is defined in a package in.india, and save as d:\myOtherProject\src\in\india\TestCircle.java.
// d:\myOtherProject\src\in\india\TestCircle.java 
packagein.india;
import in.technoscience.Circle; 
public class TestCircle { ...... }
Suppose the compiled class is to be kept as d:\myOtherProject\classes\in\india\TestCircle.class.
 
To compile TestCircle.java, set the current directory and use relative paths for TestCircle. 
d:> cd \myOtherProject 
d:\myOtherProject> javac -d classes -cp d:\myJavaProject\classes   src\com\abc\TestCircle.java 
-- To run TestCircle, need to include the base directory of TestCircle and Circle in classpath. 
-- Also need to use the fully-qualified name (package name plus class name) for TestCircle 
d:\myOtherProject> java -cp classes;d:\myJavaProject\classes  in.india.TestCircle

Follow my Upcoming Post,there  have taken one more example on classpath and quick note on classpath.

No comments:

Post a Comment