ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages


Updated on August 5, 2012

Twist your Mind



Package is a logical container which contains classes,interfaces and subpackages.

Packages are used to provide Unique Name Space to their members as well as to define a new Scope.

Name Space Providers are:

1.Technology Provider

2.Application Provider

3.Third Party Provider

In Software Development classes of multiple parties and vendors are used hence there must be a mechanism to uniquely identify classes of each vendors this problem is resolved by packages,when classes or libraries are to be distributed .they are contained in packages.

to certain that packages of different vendors don't have same name ...

following conventions are used in naming convention packages.


examples:-following are same packages of open source frameworks:-


Following steps are required to associate a class to a package:---

1. package keyword is used in the class definition to specify the package of the class.

Note:♣ package keyword when used must be the first statement of a class definition.


package pkgName;

class A


public static void main(String args[])


System.out.println("raushan get up");



2. source file and the generated class file are stored in a folder of a same name as the package.

→When classes are associated to packages then they are always refereed using there full Name.

ex: PackageName.className

How to Use Package reference in another class:

package p1;
public class A
public static void main(String args[])
System.out.println("referencing B of p2");
p2.B x=new p2.B();
System.out.println("Invoking display() of B....");

In an Application Two types of classes are referred

  1. Library Classes
  2. User Defined Classes
  • Location of Library classes is fixed and known
  • Location of user defined classes are not fixed and not known

♦ ClassPath Environment Variable is used by application developer to provide their location of their classes to the compiler and the JRE.

Difference Between Path and ClassPath

Path Environment Variable is used by the O/S to locate the specified file.
ClassPath Environment Variable is used by java compiler and class Loader to locate Class files.
from java point of view path Environment is used to locate java source file
Classpath Environment variable is used to locate classes.
Note:default value of Classpath Environment variable is current folder.

ClassPath Environment variable can be set permanently or can be specified for the compilation or execution of specific class.

Syntax:-of compiling a java classes :-

java -cp FolderToBeSearched NameOfPathOfJavaFile↑

Syntax:-of executing a java classes

java -cp FolderToBeSearched NameOfClass↑

Note:↑ is used for Enter key of keyboard.

Question:Commands to compile from following location if we have situation like this.



Commands to compile From following location:

1.E:\code\p1>javac -cp D:\↑

2.D:\>javac E:\code\p1\↑

3.C:\>javac -cp D:\;E:\code\p1\↑

Commands to execute the code :-----

1.E:\code\p1>java -cp D:\; E:\code\ p1.A↑ or java -cp ..;D:\; p1.A↑

2.D:>java -cp E:\code\;. p1.A↑

3.C:>java -cp D:\; E:\code\ p1.A↑

Twist your Mind

Question for you how to use packages :Please answer as a comment





here has dependency on and

so how to compile and run(execute) from:





4 stars from 1 rating of Packages

Which one is best language ?

See results


    0 of 8192 characters used
    Post Comment

    • gauri1234 profile image

      gauri1234 5 years ago

      this is useful..thanks