Thursday, March 27, 2014

EXPANDING JAVA CONCEPTS , part 4 - Package and classpath continued..

In Expanding Java Concept series,Part 3 ,Package And Classpath  I have explained this with few examples. Here also i will present few examples.
  
Another Example
In this example, we have more than one classes in the package and the classes reference each others. For example, the package in.technoscience.project1.subproject2 has two classes: MyClass3 and MyClass4, defined as follows:
package in.technoscience.project1.subproject2;
public class MyClass4 {   // constructor
   public MyClass4() {
      System.out.println("MyClass4 constructed");
   }
}

package in.technoscience.project1.subproject2;
public class MyClass3 {
   private MyClass4 myClass4;
   public MyClass3 () {   // constructor
      System.out.println("MyClass3 constructed");
      myClass4 = new MyClass4();   // use MyClass4 in the same package
   }
   // main() included here for testing
   public static void main(String[] args) {
      new MyClass3();
   }
}


Case 1: Source and class files in the same directory
Suppose that we keep the source and class files in the same directory, says, $BASE_DIR\in\technoscience\project1\subproject2.I assume that the CLASSPATH includes the current working directory.

To compile all the source files:

cd $BASE_DIR
javac in\technoscience\project1\subproject2\*.java

The resultant class files will be placed in the same directory as the source files.
To execute the MyClass3, you need to issue the fully-qualified class name:

cd $BASE_DIR
java in.technoscience.project1.subproject2.MyClass3 
 
you can visualize :
To compile the program, you specify the directory path using directory separator '\'.
To execute the class, you specify the fully-qualified class name using the dot '.'.

Alternatively, you can launch the class from any directory, provided that the $BASE_DIR is included in the CLASSPATH environment variable. You can also use command-line option -classpath or -cp to specify CLASSPATH used for this command:
java –cp $BASE_DIR in.technoscience.project1.subproject2.MyClass3.
Case 2: Source and class files in separate directories
Suppose that you decided to keep the source files and classes in separate directories (for distribution of classes without the sources), and the directory structure of your source files and classes is as follows
To compile the source files and place the classes in the desired directory, you can use the "-d" (for destination) command-line option of the Java compiler, which specifies the location of the compiled classes.

You also need to specify the CLASSPATH of the classes, as MyClass3 uses MyClass4, as follows:

cd $SRC_BASE_DIR\in\technoscience\project1\subproject2
javac –d $CLASS_BASE_DIR -classpath .;$CLASS_BASE_DIR *.java
// try omitting the classpath and compile just MyClass3 which uses MyClass4
> javac –d $CLASS_BASE_DIR MyClass3.java
 
The sub-directory structure corresponding to the package name for the classes will be created automatically if it does not already exist. 

In summary, during the compilation, you need to set both -d (for destination of the classes), and -classpath (if one class references other classes in the package).

In the above example, the source directory $SRC_BASE_DIR is "c:\javaproject\src" and the classes' base directory $CLASS_BASE_DIR is "c:\javaproject\classes"

To execute the MyClass3:

cd $CLASS_BASE_DIR
java in.technoscience.project1.subproject2.MyClass3
  
The Default Package
Every Java class must belong to a package. You can explicitly name the package by providing a package statement in the beginning of the source file. If the package statement is omitted, the class belongs to the so-called default package, with no sub-directory structure. Use of default package is not recommended other than writing practice program and for quick testing.

Quick Note :
CLASSPATH is an environment variable (i.e., global variables of the operating system available to all the processes) needed for the Java compiler and runtime to locate the Java packages used in a Java program. This is similar to another environment variable PATH, which is used by the CMD shell to find the executable programs.

CLASSPATH can be set in one of the following ways: 
  • CLASSPATH can be set permanently in the environment: In Windows, choose control panel ⇒ System ⇒ Advanced ⇒ Environment Variables ⇒ choose "System Variables" (for all the users) or "User Variables" (only the currently login user) ⇒ choose "Edit" (if CLASSPATH already exists) or "New" ⇒ Enter "CLASSPATH" as the variable name ⇒ Enter the required directories and JAR files (separated by semicolons) as the value (e.g., ".;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar"). Note that you need to include the current working directory (denoted by '.') in the CLASSPATH.
  • To check the current setting of the CLASSPATH, issue the following command: SET CLASSPATH.
  • CLASSPATH can be set temporarily for that particular CMD shell session by issuing the following command:
    SET CLASSPATH=.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar
    
  • Instead of using the CLASSPATH environment variable, you can also use the command-line option -classpath or -cp of the javac and java commands, for example,
    java –classpath c:\javaproject\classes in.technoscience.project1.subproject2.MyClass3
    

No comments:

Post a Comment