Create Your Own Package

The package to which the source file belongs is specified with the keyword package at the top left of the source file, before the code that defines the real classes in the package.

Create Your Own Package


The package to which the source file belongs is specified with the keyword package at the top left of the source file, before the code that defines the real classes in the package. 
Suppose we have a source file called "" and we want to put this file in a package "mypackage" then the following code is written as shown in the given example:

package mypackage;
class HelloWorld {
  public static void main (String args[]) {
  System.out.println("Hello World!");

Before running this program make sure to do the following things:

  1. Create a directory "mypackage" .
  2. Save the  source file as "" in the created directory.
  3. Set the class path as  set CLASSPATH = .;C:\;  
  4. Go to the "mypackage" directory and compile the program as
  5. Run the program.

If you try to run this program, you will get the following exceptions (or error):

C:\mypackage>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: mypackage/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at Source)
at Source)
at$000(Unknown Source)

This is, because the class "HelloWorld" belongs to the package "mypackage". So If we want to run it, we have to tell the JVM about its fully-qualified class name as (mypackage.HelloWorld) instead of its plain class name (HelloWorld). Fully-qualified class name is the name of the java class that includes its package name. 

Now run the program as shown:

C:\mypackage>java mypackage.HelloWorld
Hello World!

The ways to Compile the Package:

Compile in the same directory:  If you have a hierarchy of packages to compilation then you can compile the package without going to the subdirectories and specifying the complete directory path with the class . Suppose, you have a hierarchy of packages as "india.mycompany.mainpackage.mypackage" including the class "HelloWorld" then type the following command shown as:

C:\javac C:\india\mycompany\mainpackage\mypackage\


This command will reach to the last subdirectory and compile the class "HelloWorld".

Compile into the Different Directory: On the other hand, if you want to compile the same package available in the hierarchy manner to another directory (location) then syntax is shown as:

C:\javac -d <target_directory> <complete_directorypath>

Suppose, you want to save the compiled package to the location "D:\myfolder" then type the following command shown as:

C:\javac -d D:\myfolder C:\ india\mycompany\mainpackage\mypackage\


This command puts the folder "india" along with its subfolders and the class file "HelloWorld.class" to the new location as D:\myfolder.


Share on Google+Share on Google+

Create Your Own Package

Posted on: March 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Create Your Own Package   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 26, 2012
how do i import this package

so can i import this package simply using the name of the package or should i use the full path
March 26, 2012
how to import it

how to import this package from another class
April 23, 2012
Package Program-

Sir This is my program I saved this program in the DIR: D:\Java\jdk1.6.0\bin\Mypack\ The problem is how should i run this program using cmd? I tried your instruction but it fails to run! Please Give ME solution package Mypack; class Balance { String name; double bal; Balance(String n,double b) { name=n; bal=b; } void show() { if(bal<0) System.out.print("-->"); System.out.println(name+":$"+bal); } } class AccountBalance { public static void main(String args[]) { Balance current[]=new Balance[3]; current[0]=new Balance("K. J. Fielding",123.23); current[1]=new Balance("Will Tell",157.02); current[2]=new Balance("Tom Jackson",-12.33); for(int i=0;i<3;i++) current[i].show(); } }
May 9, 2012
plush toy pachage

big and colourful
sunit mondal
May 14, 2012

i am astudent
August 7, 2012

August 22, 2012

to run package i have to come out of the package directory then type incommand prompt,here intis case(example mypackage) c:\>java mypackage.HelloWorld
August 22, 2012
"java packages with identical method"

import two classes with identical class then you have to tell for object creation explicitly the package name for at least one class object... for example: two packages package1,package2 both containing class,named class then import package1.class1; import package2.*;// * here applicable class1.pk1=new class1(); package2.class1=new package2.class1(); here package2 explicitly told
January 30, 2014
Good Explanation

SnapShots used a lot. Thank you!!!!!!