Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

 

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (post your own) View All Comments Latest 10 Comments:

Hi, i have created directory called package1 inside that i have written "shape.java" which i have written methao for area,i Have also created one another directory called package2 in which i have written three source files "circle.java","triangle.java","rectangle.java" importing the package1.And the main function outside the thodse two package directories importing both the packages.tell me how set the class path and run that complete programm.Thanks in advance

Posted by Vicky B on Wednesday, 05.21.08 @ 21:12pm | #60609

Hi..As i followed the above procedures, codings,(except i used different package name as "project1")it compiled successflly, but when i run this, it gives, Exception in thread"main" NoClassDefFoundError: project1/HelloWorld. I created classpath using: sysedit, c:\project1;c:\program files\java\jdk1.6.0_01\bin; Can anyone help me to solve this?
Thanks in Advance
Kalpana.S

Posted by Kalpana on Monday, 11.5.07 @ 11:53am | #35564

Hi..As i followed the above procedures, codings,(except i used different package name as "project1")it compiled successflly, but when i run this, it gives, Exception in thread"main" NoClassDefFoundError: project1/HelloWorld. I created classpath using: sysedit, c:\project1;c:\program files\java\jdk1.6.0_01\bin; Can anyone help me to solve this?
Thanks in Advance
Kalpana.S

Posted by Kalpana on Monday, 11.5.07 @ 11:52am | #35563

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.