Sunday, March 6, 2016

Package in Java-3

Package in Java-3


Subpackage


Package inside the package is called the subpackage. It should be created to categorize
the package further.

Let's take an example, Sun Microsystem has defined a package named java that contains many classes
like System, String, Reader, Writer, Socket etc. These classes represent a particular group
e.g. Reader and Writer classes are for Input/Output operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun has sub categorized the java package into subpackages such as
lang, net, io etc. and put the Input/Output related classes in io package, Server and ServerSocket
classes in net packages and so on.

"The standard of defining package is domain.company.package e.g. com.abc.bean
or org.sssit.dao."

Example of Subpackage

    package com.abc.core; 
    class Simple{ 
      public static void main(String args[]){ 
       System.out.println("Hello subpackage"); 
      } 
    } 

To Compile: javac -d . Simple.java
To Run: java com.abc.core.Simple

Output:Hello subpackage

How to send the class file to another directory or drive?

There is a scenario, I want to put the class file of A.java source file in
classes folder of c: drive. For example:


//save as Simple.java 
     
    package mypack; 
    public class Simple{ 
     public static void main(String args[]){ 
        System.out.println("Welcome to package"); 
       } 
    } 


To Compile:
e:\sources> javac -d c:\classes Simple.java
To Run:
To run this program from e:\source directory, you need to set classpath of the directory where the class file resides.
e:\sources> set classpath=c:\classes;.;
e:\sources> java mypack.Simple


Another way to run this program by -classpath switch of java:

The -classpath switch can be used with javac and java tool.

To run this program from e:\source directory, you can use -classpath switch
of java that tells where to look for class file. For example:

e:\sources> java -classpath c:\classes mypack.Simple

Output:Welcome to package

No comments:

Post a Comment