---
description: A Package is a collection of related classes. Learn how to create Package in Java with example program in this tutorial
title: How to Create Packages in Java
image: https://www.guru99.com/images/how-to-create-packages-in-java.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Packages in Java are collections of related classes, sub-packages, and interfaces that organize code into a folder structure and a unique namespace, improving reusability, avoiding naming conflicts, and making classes easier to locate, create, import, and execute.

* 📦 **Package Defined:** A Java package groups related classes, interfaces, and sub-packages under one namespace.
* 🛠️ **Creating a Package:** Add the package statement as the first line, then compile with javac -d . file.java.
* 🌳 **Sub-packages:** Use dot notation such as package p1.p2; and run with the fully qualified class name.
* 📥 **Importing:** Use import package.\*; to access classes without typing their full path each time.
* 🏷️ **Naming Convention:** Reverse-domain names (e.g., com.guru99) avoid naming conflicts; java.lang is imported by default.

[ Read More ](javascript:void%280%29;) 

![How to Create Packages in Java](https://www.guru99.com/images/how-to-create-packages-in-java.png)

## What is Package in Java?

**PACKAGE in Java** is a collection of classes, sub-packages, and interfaces. It helps organize your classes into a folder structure and make it easy to locate and use them. More importantly, it helps improve code reusability. Each package in Java has its unique name and organizes its classes and interfaces into a separate namespace, or name group.

Although interfaces and classes with the same name cannot appear in the same package, they can appear in different packages. This is possible by assigning a separate namespace to each Java package.

**Syntax:-**

package nameOfPackage;

The following video takes you through the steps of creating a package.

Click [here](https://www.guru99.com/faq#faq1) if the video is not accessible   

Let’s study package with an example. We define a class and object and later compile this it in our package p1\. After compilation, we execute the code as a java package.

## How to Create a package?

Creating a package is a simple task as follows

* Choose the name of the package
* Include the package command as the first line of code in your Java Source File.
* The Source file contains the classes, interfaces, etc you want to include in the package
* Compile to create the Java packages

**Step 1)** Consider the following package program in Java:

package p1;

class c1(){
public void m1(){
System.out.println("m1 of c1");
}
public static void main(string args[]){
c1 obj = new c1();
obj.m1();
}
}

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand1.jpg)

Here,

1. To put a class into a package, at the first line of code define package p1
2. Create a class c1
3. Defining a method m1 which prints a line.
4. Defining the main method
5. Creating an object of class c1
6. Calling method m1

**Step 2)** In next step, save this file as demo.java

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand2.jpg)

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand3.jpg)

**Step 3)** In this step, we compile the file.

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand4.jpg)

The compilation is completed. A class file c1 is created. However, no package is created? Next step has the solution

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand5.jpg)

### RELATED ARTICLES

* [15 BEST Java Books for Beginners (2026 Update) ](https://www.guru99.com/books.html "15 BEST Java Books for Beginners (2026 Update)")
* [Java Variables and Data Types ](https://www.guru99.com/java-variables.html "Java Variables and Data Types")
* [String Manipulation in Java with EXAMPLE ](https://www.guru99.com/string-manipulation-in-java.html "String Manipulation in Java with EXAMPLE")
* [Top 30 Hibernate Interview Questions and Answers (2026) ](https://www.guru99.com/hibernate-interview-questions.html "Top 30 Hibernate Interview Questions and Answers (2026)")

**Step 4)** Now we have to create a package, use the command

javac –d . demo.java

This command forces the compiler to create a package.

The **“.”** operator represents the current working directory.

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand6.jpg)

**Step 5)** When you execute the code, it creates a package p1\. When you open the java package p1 inside you will see the c1.class file.

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand7.jpg)

**Step 6)** Compile the same file using the following code

javac –d .. demo.java

Here “..” indicates the parent directory. In our case file will be saved in parent directory which is C Drive

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand8.jpg)

File saved in parent directory when above code is executed.

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand9.jpg)

**Step 7)** Now let’s say you want to create a sub package p2 within our existing java package p1\. Then we will modify our code as

package p1.p2;
class c1{
public void m1() {
System.out.println("m1 of c1");
}
}

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand10.jpg)

**Step 8)** Compile the file

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand11.jpg)

As seen in below screenshot, it creates a sub-package p2 having class c1 inside the package.

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand12.jpg)

**Step 9)** To execute the code mention the fully qualified name of the class i.e. the package name followed by the sub-package name followed by the class name –

java p1.p2.c1

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand13.jpg)

This is how the package is executed and gives the output as “m1 of c1” from the code file.

[](https://www.guru99.com/images/java/052016%5F0817%5FCreatingand14.jpg)

## How to Import Package

To create an object of a class (bundled in a package), in your code, you have to use its fully qualified name.

**Example:**

java.awt.event.actionListner object = new java.awt.event.actionListner();

But, it could become tedious to type the long dot-separated package path name for every class you want to use. Instead, it is recommended you use the import statement.

**Syntax**

import packageName;

Once imported, you can use the class without mentioning its fully qualified name.

import java.awt.event.*; // * signifies all classes in this package are imported
import javax.swing.JFrame // here only the JFrame class is imported
//Usage
JFrame f = new JFrame; // without fully qualified name

**Example**: To import package

**Step 1)** Copy the code into an editor.

package p3;
import p1.*; //imports classes only in package p1 and NOT  in the sub-package p2
class c3{
  public   void m3(){
     System.out.println("Method m3 of Class c3");
  }
  public static void main(String args[]){
    c1 obj1 = new c1();
    obj1.m1();
  }
}

**Step 2)** Save the file as Demo2.java. Compile the file using the command **javac –d . Demo2.java**

**Step 3)** Execute the code using the command **java p3.c3**

### Packages – points to note:

* To avoid naming conflicts packages are given names of the domain name of the company in reverse Ex: com.guru99\. com.microsoft, com.infosys etc.
* When a package name is not specified, a class is in the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
* While creating a package, care should be taken that the statement for creating package must be written before any other import statements

// not allowed
import package p1.*;
package p3;

//correct syntax
package p3;
import package p1.*;

the _java.lang package_ is imported by default for any class that you create in Java.

The Java API is very extensive, contains classes which can perform almost all your programming tasks right from Data Structure Manipulation to Networking. More often than not, you will be using API files in your code. You can see the API documentation [here.](https://docs.oracle.com/javase/8/docs/api/index.html)

## FAQs

📚 What are the types of packages in Java?

Java has two types of packages: built-in packages (such as java.lang, java.util, and java.io provided by the Java API) and user-defined packages created by developers to organize their own classes.

⚖️ What is the difference between import package.\* and importing a single class?

An import with .\* imports all classes in a package, while importing a single class (such as javax.swing.JFrame) loads only that class. Both let you use classes without the fully qualified name.

📂 What is the default package in Java?

When no package statement is specified, a class belongs to the default package, which is the current working directory and has no name. It is mainly used for small test programs.

🤖 How can AI help organize Java packages?

AI tools can suggest logical package structures, detect misplaced classes, and recommend naming conventions. They also generate import statements automatically and flag unused imports to keep code clean.

🧠 Can AI generate Java package and class code?

Yes. AI coding assistants can scaffold packages, create classes with correct package declarations, and add import statements, helping beginners follow proper Java project structure quickly.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-create-packages-in-java.png","url":"https://www.guru99.com/images/how-to-create-packages-in-java.png","width":"700","height":"250","caption":"How to Create Packages in Java","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/packages-in-java.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/packages-in-java.html","name":"How to Create Packages in Java"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/packages-in-java.html#webpage","url":"https://www.guru99.com/packages-in-java.html","name":"How to Create Packages in Java","dateModified":"2026-06-30T16:41:22+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-create-packages-in-java.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/packages-in-java.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Java Tutorials","headline":"How to Create Packages in Java","description":"A Package is a collection of related classes. Learn how to create Package in Java with example program in this tutorial","keywords":"java","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"dateModified":"2026-06-30T16:41:22+05:30","image":{"@id":"https://www.guru99.com/images/how-to-create-packages-in-java.png"},"copyrightYear":"2026","name":"How to Create Packages in Java","subjectOf":[{"@type":"HowTo","name":"How to Create a package?","description":"Creating a package is a simple task as follows","step":[{"@type":"HowToStep","name":"Step 1) Use below package program in Java","text":"In first step Consider the following package program in Java:","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand1.jpg"},"url":"https://www.guru99.com/java-packages.html#step1"},{"@type":"HowToStep","name":"Step 2) Save this file","text":"In next step, save this file as demo.java","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand2.jpg"},"url":"https://www.guru99.com/java-packages.html#step2"},{"@type":"HowToStep","name":"Step 3) Compile the file","text":"The compilation is completed. A class file c1 is created. However, no package is created? Next step has the solution","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand4.jpg"},"url":"https://www.guru99.com/java-packages.html#step3"},{"@type":"HowToStep","name":"Step 4) Use command for creating a package","text":"Now we have to create a package, use the command. This command forces the compiler to create a package.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand6.jpg"},"url":"https://www.guru99.com/java-packages.html#step4"},{"@type":"HowToStep","name":"Step 5) Execute the code","text":"When you execute the code, it creates a package p1. When you open the java package p1 inside you will see the c1.class file.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand7.jpg"},"url":"https://www.guru99.com/java-packages.html#step5"},{"@type":"HowToStep","name":"Step 6) Compile the same file using the following code","text":"Here indicates the parent directory. In our case file will be saved in parent directory which is C Drive","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand8.jpg"},"url":"https://www.guru99.com/java-packages.html#step6"},{"@type":"HowToStep","name":"Step 7) Create a sub package p2","text":"Now let's say you want to create a sub package p2 within our existing java package p1. Then we will modify our code.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand10.jpg"},"url":"https://www.guru99.com/java-packages.html#step7"},{"@type":"HowToStep","name":"Step 8) Compile the file","text":"Now As seen in below screenshot, it creates a sub-package p2 having class c1 inside the package.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand11.jpg"},"url":"https://www.guru99.com/java-packages.html#step8"},{"@type":"HowToStep","name":"Step 9) Execute the code","text":"To execute the code mention the fully qualified name of the class i.e. the package name followed by the sub-package name followed by the class name.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/java/052016_0817_Creatingand13.jpg"},"url":"https://www.guru99.com/java-packages.html#step9"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are the types of packages in Java?","acceptedAnswer":{"@type":"Answer","text":"Java has two types of packages: built-in packages (such as java.lang, java.util, and java.io provided by the Java API) and user-defined packages created by developers to organize their own classes."}},{"@type":"Question","name":"What is the difference between import package.* and importing a single class?","acceptedAnswer":{"@type":"Answer","text":"An import with .* imports all classes in a package, while importing a single class (such as javax.swing.JFrame) loads only that class. Both let you use classes without the fully qualified name."}},{"@type":"Question","name":"What is the default package in Java?","acceptedAnswer":{"@type":"Answer","text":"When no package statement is specified, a class belongs to the default package, which is the current working directory and has no name. It is mainly used for small test programs."}},{"@type":"Question","name":"How can AI help organize Java packages?","acceptedAnswer":{"@type":"Answer","text":"AI tools can suggest logical package structures, detect misplaced classes, and recommend naming conventions. They also generate import statements automatically and flag unused imports to keep code clean."}},{"@type":"Question","name":"Can AI generate Java package and class code?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI coding assistants can scaffold packages, create classes with correct package declarations, and add import statements, helping beginners follow proper Java project structure quickly."}}]}],"@id":"https://www.guru99.com/packages-in-java.html#schema-21870","isPartOf":{"@id":"https://www.guru99.com/packages-in-java.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/packages-in-java.html#webpage"}}]}
```
