Sub topic 3
The Standard Java Project Layout
Open any Maven or Gradle project and you will see the same folders: src/main/java, src/test/java, and a few others. That is not chance. Both tools expect one shared layout. If you put your files where the tool expects them, you need almost no setup at all.
This chapter shows you that layout, explains what each folder is for, and walks you through building it by hand.
Why a Standard Layout Matters
A convention is an agreed-upon way of doing something. Maven made one popular idea well known: convention over configuration. It means the tool assumes sensible defaults, so you only write settings when you want something different.
Here is what the convention gives you:
- Less setup. The build tool already knows where your code and tests live.
- Easy onboarding. A new teammate can find things in any project right away.
- Tool support. IDEs (programs for writing code, like IntelliJ IDEA or VS Code) detect the layout and set themselves up.
- Clean separation. Test code stays out of the app you ship.
You can change these folders in your build file. Most teams never do, and you should not either unless you have a strong reason.
The Layout at a Glance
Here is a small project called hello in the standard layout:
hello/
├── pom.xml (or build.gradle.kts)
└── src/
├── main/
│ ├── java/
│ │ └── com/example/App.java
│ └── resources/
│ └── greeting.properties
└── test/
├── java/
│ └── com/example/AppTest.java
└── resources/What Each Folder Is For
| Folder | What goes in it | Shipped in the final JAR? |
|---|---|---|
| `src/main/java` | Your application's `.java` source files | Yes, as compiled `.class` files |
| `src/main/resources` | Non-code files your app reads, such as `.properties` or `.xml` config | Yes, copied as-is |
| `src/test/java` | Test classes, such as JUnit tests | No |
| `src/test/resources` | Files only the tests need, such as sample data | No |
A source file is a .java file you write. A class file is the .class file the compiler makes from it. A resource is any other file your program needs while it runs, like a settings file or a text template.
Main versus test
The main side holds what your users run. The test side holds code that checks the main side. Tests can see and use main code. Main code can never see test code. This one-way rule keeps test helpers from leaking into your real app.
Java versus resources
Files in java/ get compiled: they are turned into class files. Files in resources/ are only copied, unchanged, next to the class files. Both end up on the classpath, which is the list of places Java looks for classes and files while your program runs.
Package Folders Must Match Package Names
A package is a named group of classes, declared at the top of a Java file. The file App.java starts with:
package com.example;So it must live in the folders com/example/ under src/main/java. Each dot in the package name becomes a folder. Here is how a few package names map to paths:
| Package | Folder under `src/main/java` |
|---|---|
| `com.example` | `com/example/` |
| `com.example.shop` | `com/example/shop/` |
| `org.acme.util` | `org/acme/util/` |
Tests usually use the same package as the class they test. That is why AppTest.java lives in src/test/java/com/example/. Being in the same package lets a test call methods that have no public keyword, like App.greeting.
Where the Output Goes
The build tool needs somewhere to put the files it makes. Maven and Gradle choose different folders:
| Tool | Output folder | Compiled main classes end up in |
|---|---|---|
| Maven | `target/` | `target/classes/` |
| Gradle | `build/` | `build/classes/java/main/` |
These output folders are generated: the tool can rebuild them from your sources at any time. Never edit files inside them, and never commit them to version control. A .gitignore file tells Git which files to skip:
target/
build/
.gradle/Worked Example: Build the Layout Yourself
Let's create the layout, add code and a resource, and see why resources need to be copied. Run these commands in an empty folder:
mkdir -p hello/src/main/java/com/example
mkdir -p hello/src/main/resources
mkdir -p hello/src/test/java/com/example
mkdir -p hello/src/test/resources
cd helloThe -p flag makes every parent folder that does not exist yet.
Now add a resource file at src/main/resources/greeting.properties:
name=LayoutThen add the main class at src/main/java/com/example/App.java. This version reads the name from the resource file:
package com.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App {
public static void main(String[] args) throws IOException {
Properties props = new Properties();
try (InputStream in = App.class.getResourceAsStream("/greeting.properties")) {
if (in == null) {
System.out.println("greeting.properties not found on the classpath");
return;
}
props.load(in);
}
System.out.println(greeting(props.getProperty("name")));
}
static String greeting(String name) {
return "Hello, " + name + "!";
}
}The call getResourceAsStream("/greeting.properties") searches the classpath for that file. The leading / means "start at the top of the classpath."
Now do by hand what a build tool does for you. Compile the Java code, then copy the resources into the same output folder:
mkdir -p out
javac -d out src/main/java/com/example/App.java
java -cp out com.example.App
cp -r src/main/resources/. out/
java -cp out com.example.AppThe first java run prints greeting.properties not found on the classpath, because only the class file is in out/. After the copy, the second run prints Hello, Layout!. Maven and Gradle do this copy step for you every time they build.
Finally, the test goes at src/test/java/com/example/AppTest.java:
package com.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class AppTest {
@Test
void greetsByName() {
assertEquals("Hello, Maven!", App.greeting("Maven"));
}
}You will run this test with a build tool in a later chapter. For now, notice its location: same package, but under test instead of main.
Common Mistakes
- Wrong folder depth. Putting
App.javastraight intosrc/main/javawhile it sayspackage com.example;. The build tool will complain or put the class in the wrong place. - Typos in folder names.
src/main/Javaorsrc/tests/javawill not be found. The names are case-sensitive on most systems. - Tests in main. A test class in
src/main/javagets shipped to users. It also fails to compile, because JUnit is only available to test code. - Resources in the wrong place. Config files under
java/instead ofresources/. - Committing output. Checking
target/orbuild/into Git bloats the repository and causes confusing conflicts.
Exercise
A class starts with "package org.acme.billing;" and is named Invoice. Where should Invoice.java go, and where should its test, InvoiceTest.java, go?
Show answer
Invoice.java goes in src/main/java/org/acme/billing/Invoice.java. InvoiceTest.java goes in src/test/java/org/acme/billing/InvoiceTest.java, using the same package so it can reach non-public methods.
Exercise
Your app reads a file named sample-orders.csv, but only your tests use it. Which folder should it go in, and will it be included in the shipped JAR?
Show answer
Put it in src/test/resources. It will be on the classpath while tests run, but it is not packaged into the final JAR.
Exercise
In the worked example, why did the first "java -cp out com.example.App" run fail to find greeting.properties?
Show answer
javac only compiles .java files into out/. It does not copy resource files. The properties file was not on the classpath until it was copied from src/main/resources into out/. Build tools do this copy step automatically.
Exercise
Which of these folders should you add to .gitignore: src/main/resources, target, build, src/test/java?
Show answer
target and build. They hold generated output that the build tool can recreate. The src folders hold your real source and must be committed.