When you are learning Java, you might write everything inside one file. That's completely fine for small programs.
But as your application grows, you'll have many classes, packages, configuration files, tests, and other resources. Keeping everything in one place quickly becomes messy.
That's why Java projects usually follow a proper project structure.
A simple Java project might look like this:
MyJavaProject/
│
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── example/
│ └── app/
│ └── Main.java
│
└── README.mdDon't worry if this looks complicated. Let's understand each part one by one.
The Project Folder
At the top, we have the main project folder:
MyJavaProject/This is simply the folder that contains everything related to our Java application.
For a small project, it could contain:
MyJavaProject/
├── src/
├── README.md
└── ...The exact structure depends on the type of project and the tools you're using.
The src Folder
src stands for source.
This is where we normally keep the source code of our application.
For example:
src/
└── main/
└── java/The actual Java files will eventually be placed inside this directory.
The main Folder
In a standard Maven or Gradle project, you'll commonly see:
src/main/The main folder contains the main application code and resources.
Inside it, there is usually:
src/main/java/
src/main/resources/The java folder contains Java source files, while resources is used for non-Java files needed by the application.
The java Folder
The Java source code goes inside:
src/main/java/For example:
src/
└── main/
└── java/
└── Main.javaOur Main.java file could contain:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, John!");
}
}In projects that use packages, you'll normally see the package structure inside the java folder.
Packages
Suppose our package is:
package com.example.app;The folder structure would normally look like:
src/
└── main/
└── java/
└── com/
└── example/
└── app/
└── Main.javaSo the package:
com.example.appcorresponds to:
com/example/appThis is why understanding packages is important when working with real Java projects.
Multiple Packages
A larger application might have several packages for different parts of the application.
For example:
src/main/java/com/example/app/
│
├── model/
│ ├── User.java
│ └── Product.java
│
├── service/
│ ├── UserService.java
│ └── ProductService.java
│
├── controller/
│ └── UserController.java
│
└── Main.javaHere, related classes are grouped together.
For example, User.java might belong to:
package com.example.app.model;while UserService.java might belong to:
package com.example.app.service;This keeps larger applications easier to understand and maintain.
The resources Folder
The resources folder is commonly used for files that aren't Java source code.
For example:
src/main/resources/
├── config.properties
├── messages.txt
└── images/These files can be loaded by the application when needed.
You might find configuration files, templates, SQL files, images, or other resources here depending on the project.
The test Folder
Standard Java projects often have a separate folder for testing:
src/
├── main/
│ ├── java/
│ └── resources/
│
└── test/
└── java/The src/test/java directory contains test code.
For example:
src/test/java/
└── com/example/app/
└── UserTest.javaKeeping tests separate from application code makes the project easier to manage.
Maven Project Structure
If you're using Maven, you'll commonly see a structure like this:
MyJavaProject/
│
├── src/
│ ├── main/
│ │ ├── java/
│ │ └── resources/
│ │
│ └── test/
│ ├── java/
│ └── resources/
│
├── pom.xml
└── README.mdThe important file here is:
pom.xmlThis is Maven's project configuration file.
It can contain information such as project details, dependencies, plugins, and build configuration.
For example, if your application needs a third-party library, you can declare it as a dependency in pom.xml.
Gradle Project Structure
Gradle projects have a similar structure:
MyJavaProject/
│
├── src/
│ ├── main/
│ │ ├── java/
│ │ └── resources/
│ │
│ └── test/
│ ├── java/
│ └── resources/
│
├── build.gradle
└── settings.gradleInstead of pom.xml, Gradle commonly uses files such as:
build.gradle
settings.gradleThese files contain the project's build configuration and dependencies.
You don't need to memorize Maven or Gradle right now. The important thing is to recognize the standard structure when you see it.
A Small Real-World Example
Imagine we're building a simple student management application.
We could organize it like this:
StudentApp/
│
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── example/
│ └── studentapp/
│ │
│ ├── model/
│ │ └── Student.java
│ │
│ ├── service/
│ │ └── StudentService.java
│ │
│ └── Main.java
│
└── README.mdStudent.java could contain the student data and behavior.
StudentService.java could contain operations related to students.
Main.java could start the application.
Instead of putting everything into one huge class, each part has a clear place.
Why Project Structure Matters
A good project structure makes your application easier to:
Understand
Navigate
Maintain
Test
Expand
Work on with other developers
When your project has only one or two classes, structure might not seem very important.
But imagine having 500 Java files in one folder. Finding anything would become a nightmare.
That's why real-world Java applications use packages, source folders, resource folders, and testing directories to keep everything organized.
The main idea to remember is:
Java project structure is simply a way of organizing your source code, packages, resources, tests, and project configuration so that a project remains clean and manageable as it grows.