Running your app and building a JAR
40 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Your app runs fine from the IDE, but a friend who has only Java cannot start it. Running from the terminal, and shipping the app as one file, each need a little extra setup.
Running Main from the IDE versus from the terminal
Run Main from the IDE and from the terminal, and compare. The program needs Commons Text, so the classpath decides the result.
Example 1 · src/main/java/com/college/app/Main.java, run without its libraries
Runs in: the receipt-app Maven project
package com.college.app;
import org.apache.commons.text.WordUtils;
public class Main { public static void main(String[] args) { String typed = args.length > 0 ? args[0] : "asha RAO"; String name = WordUtils.capitalizeFully(typed); int total = new FeeCalculator().total(85000, 42000, 20);
System.out.println("Hostel fee receipt"); System.out.println("Student: " + name); System.out.println("Total: Rs " + total); }}$ mvn -q compile $ java -cp target/classes 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)
What this code does
- In IntelliJ the Run button prints the receipt, because the IDE adds Commons Text and Commons Lang from
~/.m2to the classpath. - In the terminal
target/classesalone is not enough: Java cannot findWordUtils.
The libraries sit in ~/.m2/repository, not on the terminal classpath, so the program crashes the moment it needs one. You could type the long -cp by hand, as in the first lesson of this module, but that brings back every problem Maven was meant to solve.
Two clean terminal options follow: let Maven build the classpath (exec:java), or build one JAR that contains everything (the shade plugin).
mvn exec:java with the exec-maven-plugin and a mainClass
The exec-maven-plugin runs a class's main inside Maven, with the project's full runtime classpath. Its goal exec:java starts the program.
Add the plugin to the pom with a <mainClass>. Then mvn compile exec:java compiles and runs your app with every library on the classpath.
Example 2 · exec-maven-plugin: run Main with mvn exec:java
Runs in: the receipt-app Maven project
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.6.4</version> <configuration> <mainClass>com.college.app.Main</mainClass> </configuration> </plugin> </plugins> </build>$ mvn compile exec:java [INFO] --- exec:3.6.4:java (default-cli) @ receipt-app --- Hostel fee receipt Student: Asha Rao Total: Rs 110000 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.806 s [INFO] Finished at: 2026-10-16T10:23:37+05:30 [INFO] ------------------------------------------------------------------------ $ mvn -q compile exec:java "-Dexec.args='ravi KUMAR'" Hostel fee receipt Student: Ravi Kumar Total: Rs 110000 $ mvn -q exec:java "-Dexec.args=meena" Hostel fee receipt Student: Meena Total: Rs 110000
What this code does
- The plugin's
<mainClass>says whichmainto run; Maven supplies the classpath with all dependencies, soWordUtilsis found. -qleaves only the program's output.'ravi KUMAR'arrives as one argument and is capitalised asRavi Kumar; withoutcompile(last run) Maven reuses the classes already intarget.
exec:java is a goal, so on its own it compiles nothing: always put compile before it. Program arguments go in exec.args; quote the whole option for PowerShell, and put single quotes around one argument that contains spaces.
Building a JAR with mvn package and running it with java -jar
The manifest is the small file META-INF/MANIFEST.MF inside every JAR. java -jar reads its Main-Class line to find where the program starts.
mvn package builds target/<artifactId>-<version>.jar. Run that JAR with java -jar and see what Maven's default JAR is missing.
Example 3 · java -jar on Maven's default JAR
Runs in: a terminal in the receipt-app Maven project (no jar or shade configuration)
mvn -q clean packagejava -jar target/receipt-app-1.0-SNAPSHOT.jar$ mvn -q clean package $ java -jar target/receipt-app-1.0-SNAPSHOT.jar no main manifest attribute, in target/receipt-app-1.0-SNAPSHOT.jar
What this code does
- The JAR was built and the tests passed, but its manifest has no
Main-Classline. java -jarneeds that line to know where to start, so it stops before running any of your code.
Maven's default JAR is a library JAR: compiled classes and a manifest with no Main-Class. Maven cannot guess your entry point, because any class may have a main, so java -jar stops at once with "no main manifest attribute".
The JAR still works as a library, or with an explicit class name such as java -cp target/receipt-app-1.0-SNAPSHOT.jar com.college.app.Main, but that again needs the library JARs on the classpath.
'no main manifest attribute' and Main-Class in the jar plugin
The maven-jar-plugin builds the JAR in the package phase. Its <archive><manifest><mainClass> setting writes Main-Class into the manifest.
Tell the jar plugin which class has main. The first error goes away, and a new one appears, because the JAR names its main class but carries no libraries.
Example 4 · maven-jar-plugin with mainClass: runnable, but libraries missing
Runs in: the receipt-app Maven project (inside <build><plugins>)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.5.1</version> <configuration> <archive> <manifest> <mainClass>com.college.app.Main</mainClass> </manifest> </archive> </configuration> </plugin>$ mvn -q clean package $ java -jar target/receipt-app-1.0-SNAPSHOT.jar 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 more $ unzip -p target/receipt-app-1.0-SNAPSHOT.jar META-INF/MANIFEST.MF Manifest-Version: 1.0 Created-By: Maven JAR Plugin 3.5.1 Java-Version: 17 Build-Jdk-Spec: 21 Main-Class: com.college.app.Main
What this code does
- The manifest now has
Main-Class: com.college.app.Main, sojava -jarstartsmain. - The next line of
mainneedsWordUtils, which is not inside the JAR:NoClassDefFoundError. A runnable JAR also needs its libraries.
For an app with no libraries, the jar plugin alone is enough. Nearly every real app has libraries, so it needs the next step.
Fix the error
Fix the main class
The jar plugin cannot start the program. Pick the corrected line.
The class Main is in the package com.college.app.
Error
Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main
A runnable JAR that includes its dependencies: the shade plugin
An uber JAR (fat JAR) is one JAR that holds your classes and the classes of every runtime dependency. java -jar runs it on any computer that has Java.
The shade plugin builds the uber JAR during package, and the pom below also sets its main class. Then one file runs anywhere with Java 17 or newer.
Example 5 · maven-shade-plugin: one JAR with everything inside
Runs in: the receipt-app Maven project (inside <build><plugins>)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.6.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.college.app.Main</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </transformers> </configuration> </execution> </executions> </plugin>$ mvn clean package [INFO] --- jar:3.5.1:jar (default-jar) @ receipt-app --- [INFO] Building jar: C:\Users\asha\projects\receipt-app\target\receipt-app-1.0-SNAPSHOT.jar [INFO] --- shade:3.6.2:shade (default) @ receipt-app --- [WARNING] commons-lang3-3.20.0.jar, commons-text-1.15.0.jar, mysql-connector-j-26.7.0.jar, protobuf-java-4.31.1.jar, receipt-app-1.0-SNAPSHOT.jar define 1 overlapping resource: [WARNING] - META-INF/MANIFEST.MF [WARNING] commons-lang3-3.20.0.jar, commons-text-1.15.0.jar define 3 overlapping classes and resources: [WARNING] - META-INF.versions.9.module-info [WARNING] - META-INF/LICENSE.txt [WARNING] - META-INF/NOTICE.txt [WARNING] maven-shade-plugin has detected that some files are [WARNING] present in two or more JARs. When this happens, only one [WARNING] single version of the file is copied to the uber jar. [WARNING] Usually this is not harmful and you can skip these warnings, [WARNING] otherwise try to manually exclude artifacts based on [WARNING] mvn dependency:tree -Ddetail=true and the above output. [WARNING] See https://maven.apache.org/plugins/maven-shade-plugin/ [INFO] Replacing original artifact with shaded artifact. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.581 s [INFO] Finished at: 2026-10-16T10:24:33+05:30 [INFO] ------------------------------------------------------------------------
What this code does
- The
<execution>attaches theshadegoal to thepackagephase, so a normalmvn packagebuilds the uber JAR right after the plain one. createDependencyReducedPomfalse stops shade from writing an extradependency-reduced-pom.xmlinto the project folder. The warnings list files that exist in several JARs; one copy of each is kept.
Example 6 · Run the uber JAR anywhere with java -jar
Runs in: a terminal in the receipt-app Maven project with the shade plugin
ls target/*.jarjava -jar target/receipt-app-1.0-SNAPSHOT.jarjava -jar target/receipt-app-1.0-SNAPSHOT.jar "meena IYER"$ ls target/*.jar target/original-receipt-app-1.0-SNAPSHOT.jar target/receipt-app-1.0-SNAPSHOT.jar $ java -jar target/receipt-app-1.0-SNAPSHOT.jar Hostel fee receipt Student: Asha Rao Total: Rs 110000 $ java -jar target/receipt-app-1.0-SNAPSHOT.jar "meena IYER" Hostel fee receipt Student: Meena Iyer Total: Rs 110000
What this code does
- The uber JAR is about 5.5 MB because it contains Commons Text, Commons Lang, the MySQL driver and Protocol Buffers; the thin
original-JAR is about 4 KB. - Arguments after the JAR name become
args, so"meena IYER"printsMeena Iyer. Copy this one file to another computer with Java 17+ and it runs the same way.
The maven-shade-plugin unpacks each dependency JAR and repacks everything into yours, and its ManifestResourceTransformer writes Main-Class into the manifest. Add ServicesResourceTransformer too: JDBC drivers register themselves through files in META-INF/services, and this transformer merges those files, so the MySQL driver is still found inside the uber JAR.
The original thin JAR is kept as original-...jar. Shade warns about "overlapping resources" such as LICENSE.txt or MANIFEST.MF that exist in several JARs; these are usually harmless, as the message itself says.
Next step
Check your understanding
11 quick questions