Home Java Master-java Create Your Own Package
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Create Your Own Package

Advertisement
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 "HelloWorld.java" 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 "HelloWorld.java" in the created directory.
  3. Set the class path as  set CLASSPATH = .;C:\;  
  4. Go to the "mypackage" directory and compile the program as
    C:\mypackage>javac HelloWorld.java
  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 java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$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\HelloWord.java

 

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\HelloWord.java

 

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

 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 13, 2008

Ask Questions?    Discuss: Create Your Own Package   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
JEDI
March 30, 2011
Creating Own Package

One should have to restart their command prompt to execute the java file contained in the package after setting the CLASSPATH.
Dipali Hadkar
April 14, 2011
Thank you

Very good explanation
venkatesan.g
August 22, 2011
create package

learn something from above content.
monika
August 27, 2011
hoew to compile package

thnx it help me
Ginna Yadav
November 25, 2011
Using mypackage.HelloWorld to run your program

You've said run the class HelloWorld from mypackage by typing the following: C:\mypackage> java mypackage.HelloWorld This will again introduce java.lang.NoClassFoundError as we are telling the JVM to find HelloWorld inside mypackage, which inturn is inside folder called mypackage. Thus the command should be: C:\java mypackage.HelloWorld
tara
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
tara
March 26, 2012
how to import it

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

Sir This is my program I saved this program in the DIR: D:\Java\jdk1.6.0\bin\Mypack\AccountBalance.java 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(); } }
rosie
May 9, 2012
plush toy pachage

big and colourful
sunit mondal
May 14, 2012
java

i am astudent
sagar
August 7, 2012
sagar

hello
padmarag
August 22, 2012
java

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
padmarag
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
Prathap's
January 30, 2014
Good Explanation

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