What a build tool does and why Maven
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A college receipt app needs one text helper from a library. By hand, a single forgotten JAR crashes the program, and every classmate ends up with different versions. Teams use a build tool so that one command works for everyone.
Compiling by hand with javac and java -cp
A library is code someone else wrote and shared as a JAR file. Your program uses its classes, so the JAR must be on the classpath of both the compiler (javac) and the JVM (java).
The receipt program below uses WordUtils from the Apache Commons Text library, which turns asha RAO into Asha Rao. Compiling by hand means typing javac and java yourself, with every JAR listed after -cp.
Example 1 · src/com/college/app/Main.java: a program that uses a library
Runs in: the receipt-app folder with lib/commons-text-1.15.0.jar and lib/commons-lang3-3.20.0.jar
package com.college.app;
import org.apache.commons.text.WordUtils;
public class Main { public static void main(String[] args) { String typed = "asha RAO"; String name = WordUtils.capitalizeFully(typed);
System.out.println("Hostel fee receipt"); System.out.println("Student: " + name); System.out.println("Amount: Rs 42000"); }}Hostel fee receipt Student: Asha Rao Amount: Rs 42000
What this code does
WordUtilsis not part of the JDK. It lives in the Commons Text JAR, so the compiler and the JVM must both be told where that JAR is.- The output is what you get once every JAR is on the classpath; the next two examples show what happens before that.
Example 2 · Compile and run by hand: the JAR is needed twice
Runs in: the receipt-app folder (Windows, Git Bash)
javac -d out src/com/college/app/Main.javajavac -d out -cp lib/commons-text-1.15.0.jar src/com/college/app/Main.javajava -cp out com.college.app.Main$ javac -d out src/com/college/app/Main.java
src\com\college\app\Main.java:3: error: package org.apache.commons.text does not exist
import org.apache.commons.text.WordUtils;
^
src\com\college\app\Main.java:8: error: cannot find symbol
String name = WordUtils.capitalizeFully(typed);
^
symbol: variable WordUtils
location: class Main
2 errors
$ javac -d out -cp lib/commons-text-1.15.0.jar src/com/college/app/Main.java
$ java -cp out com.college.app.Main
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/text/WordUtils
at com.college.app.Main.main(Main.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.text.WordUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
... 1 moreWhat this code does
- Without
-cp,javaccannot see the library: "package org.apache.commons.text does not exist". - With the JAR on the compile classpath,
javacsucceeds and prints nothing. Butjava -cp outleaves the JAR out again, so the program crashes at line 8 withNoClassDefFoundError.
The compiler only checks that the classes exist. Forget the JAR when you run, and the program compiles fine and then crashes with NoClassDefFoundError the moment it needs the missing class. Doing it by hand works well for a few files with no libraries, as in the Running Java outside the IDE module.
The classpath separator is ; on Windows and : on macOS and Linux, for example "out:lib/commons-text-1.15.0.jar". Keep the quotes in every shell.
Predict the output
Which error do you get?
Both commands run in the receipt-app folder. Pick what happens.
Round 1 of 2
The library JAR is never mentioned.
Copying JAR files around: transitive JARs and versions
A transitive dependency is a JAR that one of your libraries needs. Your code never mentions it, but the program cannot run without it.
Commons Text calls Apache Commons Lang inside, so the app also needs commons-lang3. Watch what the first run below prints.
Example 3 · The hidden JAR: commons-text needs commons-lang3
Runs in: the receipt-app folder (Windows, Git Bash)
java -cp "out;lib/commons-text-1.15.0.jar" com.college.app.Mainjava -cp "out;lib/commons-text-1.15.0.jar;lib/commons-lang3-3.20.0.jar" com.college.app.Main$ java -cp "out;lib/commons-text-1.15.0.jar" com.college.app.Main Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils at org.apache.commons.text.WordUtils.capitalizeFully(WordUtils.java:250) at org.apache.commons.text.WordUtils.capitalizeFully(WordUtils.java:221) at com.college.app.Main.main(Main.java:8) Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ... 3 more $ java -cp "out;lib/commons-text-1.15.0.jar;lib/commons-lang3-3.20.0.jar" com.college.app.Main Hostel fee receipt Student: Asha Rao Amount: Rs 42000
What this code does
- The first run has Commons Text but crashes inside
WordUtils.capitalizeFully(line 250 of the library), because that method callsStringUtilsfrom Commons Lang. - Your code never mentions Commons Lang, yet the program only works once its JAR is added. That is a transitive dependency, found the hard way.
Nothing inside a JAR tells java what else to fetch. You find out from a crash, one missing JAR at a time, and then you must guess a version that fits: Commons Text 1.15.0 was built against Commons Lang 3.20.0, and a much older Lang may lack the methods it calls.
Copying JARs by hand also spreads different versions around. One classmate has Lang 3.20.0, another has 3.12.0 from an old project, and the program runs on one laptop only: the classic works on my machine problem. Real projects need many JARs: JUnit alone brings 8, and the MySQL driver brings one more (Protocol Buffers).
Fix the error
Fix the run command
The program compiled, but the run fails. Pick the line that fixes it.
Commons Text is on the classpath, yet the receipt crashes.
Error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
What Maven does
A build tool is a program that downloads libraries, compiles, tests and packages a project with one command. Maven is the build tool most Java projects use.
In Maven, one file named pom.xml lists the library and its version. Maven reads it, downloads what is needed and compiles. The first example is the receipt app described that way.
Example 4 · pom.xml: the same project described for Maven
Runs in: the receipt-app Maven project
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.college</groupId> <artifactId>receipt-app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.release>17</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.15.0</version> </dependency> </dependencies></project>(pom.xml is a description of the project; it prints nothing by itself. Run mvn compile next to it, as in the next example.)
What this code does
groupId,artifactIdandversionname your project. The<dependency>block names the one library you use directly; the pom lesson explains every line.- There is no entry for Commons Lang. Maven reads the Commons Text pom on Maven Central, sees that it needs Commons Lang 3.20.0 and fetches it too.
Example 5 · mvn compile: libraries downloaded for you
Runs in: a terminal in the receipt-app Maven project
mvn compile$ mvn compile [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-text/1.15.0/commons-text-1.15.0.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-text/1.15.0/commons-text-1.15.0.pom (24 kB at 31 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.20.0/commons-lang3-3.20.0.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.20.0/commons-lang3-3.20.0.pom (33 kB at 571 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-text/1.15.0/commons-text-1.15.0.jar Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-text/1.15.0/commons-text-1.15.0.jar (265 kB at 2.1 MB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.20.0/commons-lang3-3.20.0.jar Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.20.0/commons-lang3-3.20.0.jar (714 kB at 2.7 MB/s) [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] skip non existing resourceDirectory C:\Users\asha\projects\receipt-app\src\main\resources [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] Recompiling the module because of changed dependency. [INFO] Compiling 1 source file with javac [debug release 17] to target\classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.313 s [INFO] Finished at: 2026-10-16T08:14:33+05:30 [INFO] ------------------------------------------------------------------------
What this code does
- Maven downloads
commons-textand, without being asked,commons-lang3. The.pomfiles are how it learns what each library needs. - Then it compiles
src/main/javaintotarget/classesfor Java 17. Run it again and the download lines are gone: the JARs are now stored on your computer.
POM stands for Project Object Model: a description of your project in XML, a text format with tags such as <version>. Maven downloads each library from Maven Central, the main public store of Java libraries, together with its transitive dependencies, and puts every JAR on the classpath.
The commands are the same for everyone (mvn compile, mvn test, mvn package). A classmate who clones your repository gets the same result, with no lib folder in Git and no long -cp. IntelliJ IDEA, VS Code and Eclipse read pom.xml and set themselves up from it.
Order of execution
What happens on a new laptop
A classmate clones your project. Put the steps in order.
Convention over configuration: the standard layout
Convention over configuration means a tool assumes sensible defaults, so you write down only what is different. Maven expects code, tests and output in fixed folders.
Maven looks for your code in a fixed place instead of asking where it is. The standard layout of the receipt app is below.
Example 6 · The standard Maven layout
receipt-app/ pom.xml the project file src/ main/ java/ your Java code, in package folders com/college/app/Main.java resources/ settings and data files for the app test/ java/ your JUnit tests com/college/app/FeeCalculatorTest.java target/ everything Maven builds (made by Maven, never committed) classes/com/college/app/Main.class receipt-app-1.0-SNAPSHOT.jar(A folder layout, not a program. Every Maven project looks like this.)
What this code does
src/mainis the app,src/testis the tests, and the same package folders (com/college/app) appear under both.targetis made by Maven on every build, so it is never edited by hand and never committed to Git.
Files the app reads at run time, such as settings, go in src/main/resources. The flip side of the default layout: code placed anywhere else, such as src/com/..., is simply not compiled. The next lesson shows that mistake and its fix.
Installing Maven and checking it with mvn -v
JAVA_HOME is an environment variable holding the folder of your JDK, not its bin folder. Maven uses it to find the Java it runs and compiles with.
Maven is a Java program, so it needs a JDK first. It finds the JDK through JAVA_HOME or, if that is not set, through the java on your PATH. After installing, open a new terminal and run these two commands.
java -versionmvn -vDownload Maven from the Download page of maven.apache.org: the newest 3.x release (this course uses 3.9.16), as the binary zip (Windows) or tar.gz (macOS) archive, never the source archive or an alpha, beta or rc version. On Windows, unzip it, for example to C:\Program Files\apache-maven-3.9.16, and add its bin folder to Path under Environment Variables, setting JAVA_HOME there too. On macOS, brew install maven is the easiest way, and export JAVA_HOME=$(/usr/libexec/java_home) in ~/.zshrc sets JAVA_HOME.
'mvn' is not recognizedorcommand not found: mvn→ Maven'sbinfolder is not on PATH, or the terminal was opened before you changed it.The JAVA_HOME environment variable is not defined correctly→JAVA_HOMEpoints to a wrong folder, often one ending inbin.mvn -vshows a different Java thanjava -version→ setJAVA_HOMEto the JDK you want, then open a new terminal.
Next step
Check your understanding
11 quick questions