Real Java projects have hundreds of files, use other people's libraries and must build the same way on every machine. A few standard tools make that work, and every Java job uses them.
Packages and layout
Classes are grouped into packages, named like a reversed web address so they don't clash: com.example.shop. The first line of a file says its package, and the folders must match. Maven and Gradle both expect the same layout:
To use a class from another package, import com.example.shop.OrderService; at the top. A class's full name, package and all, is what you'll see in stack traces and logs.
Maven and Gradle
A build tool downloads the libraries you depend on, compiles your code, runs the tests and packages it all into a jar (a zip of compiled classes). Maven is configured in pom.xml, Gradle in build.gradle. A dependency is named by a group, an artifact and a version, and comes from Maven Central, Java's public library store.
$ mvn test # compile and run the tests
$ mvn package # ...and build target/shop-1.0.0.jar
$ ./gradlew build # Gradle: compile, test and build build/libs/*.jar
$ java -jar target/shop-1.0.0.jar
<scope>test</scope> means JUnit is only used by the tests and doesn't go into the final jar. The ./gradlew script (the Gradle wrapper) downloads the exact Gradle version the project needs, so everyone builds the same way.
Testing code that talks to others
Good tests are fast and give the same answer every time, so they mustn't depend on a real database, the network or today's date. The trick is to give your class what it needs through its constructor, as an interface (this is dependency injection). Real code passes the real thing; a test passes a small fake:
Java
public class Main {
interface Clock { int hour(); }
interface Mailer { void send(String to, String text); }
static class Greeter {
private final Clock clock;
private final Mailer mailer;
Greeter(Clock clock, Mailer mailer) { this.clock = clock; this.mailer = mailer; }
void greet(String who) {
String hello = clock.hour() < 12 ? "Good morning" : "Good afternoon";
mailer.send(who, hello + ", " + who);
}
}
public static void main(String[] args) {
List<String> sent = new ArrayList<>();
Mailer fake = (to, text) -> sent.add(text);
new Greeter(() -> 9, fake).greet("Ana");
new Greeter(() -> 15, fake).greet("Ben");
System.out.println(sent);
}
}
Prints
[Good morning, Ana, Good afternoon, Ben]
Because Clock and Mailer are functional interfaces, a lambda is a whole fake. The test controls the time and records the mail instead of sending it. Libraries like Mockito make such stand-ins (mocks) for bigger interfaces. In JUnit the check would be assertEquals(List.of("Good morning, Ana"), sent);.
Logging
System.out.println is fine for learning; real services log. A logging library (SLF4J in front of Logback or Log4j is the common setup) adds the time, the level, the thread and the class to every line, and lets you turn detail up or down without changing code. Levels from quiet to loud: ERROR, WARN, INFO, DEBUG, TRACE.
OrderService.java
private static final Logger log = LoggerFactory.getLogger(OrderService.class);
void place(Order order) {
log.info("placing order {} for {}", order.id(), order.customer());
try {
payments.charge(order);
} catch (PaymentException e) {
log.error("payment failed for order {}", order.id(), e); // the exception last: its stack trace is logged
throw e;
}
}
Log
2026-11-25 09:14:03.512 INFO [http-7] c.e.shop.OrderService - placing order 1042 for ana
2026-11-25 09:14:03.890 ERROR [http-7] c.e.shop.OrderService - payment failed for order 1042
com.example.shop.PaymentException: card declined
at com.example.shop.Payments.charge(Payments.java:57)
The {} placeholders are filled in only if the line is actually logged, which is cheaper than joining strings. Log what someone would need to find the problem (ids, not whole objects), and never log passwords, card numbers or other personal data.
When a build behaves differently on two machines, compare java -version, the build tool's version and mvn dependency:tree (which library versions were really used). That answers most "works on my machine" puzzles.
Practice
A class starts with package com.example.billing;. Where does its source file go in a Maven project?