A JAR (Java ARchive) file is a ZIP file that bundles compiled .class files, resources such as properties files and images, and a manifest that describes the contents. Every library in your pom.xml arrives as a JAR, and the Selenium Grid server is one you run directly. This guide shows how to create, inspect and run JAR files, and how to build them with Maven.

What's Inside a JAR

myapp.jar
├── META-INF/
│   └── MANIFEST.MF          metadata, e.g. Main-Class and Class-Path
├── com/example/App.class    compiled classes, in package folders
└── config.properties        resources

Because it's a ZIP file, you can open a JAR with any archive tool to look inside.

Advertisement

Create a JAR with the jar Tool

javac -d out src/com/example/App.java          # compile into the out/ folder
jar cf myapp.jar -C out .                       # c = create, f = file name
jar tf myapp.jar                                # t = list the contents
jar xf myapp.jar                                # x = extract

Executable JARs and the Manifest

To run a JAR with java -jar, its manifest must name the class with the main method. The e option sets it for you:

jar cfe myapp.jar com.example.App -C out .
java -jar myapp.jar

The resulting META-INF/MANIFEST.MF contains a line like Main-Class: com.example.App. Without it, java -jar fails with no main manifest attribute.

Running Classes from a JAR

java -jar myapp.jar                              # uses Main-Class from the manifest
java -cp myapp.jar com.example.App               # names the class yourself
java -cp "myapp.jar:lib/*" com.example.App       # with dependency JARs (use ; on Windows)

A plain JAR doesn't include its dependencies. If App uses Selenium, the Selenium JARs must also be on the classpath, or the program fails with NoClassDefFoundError.

Building JARs with Maven

mvn package compiles the code, runs the tests and writes a JAR to target/. To produce one self-contained "fat" JAR that includes all dependencies and runs with java -jar, use the Maven Shade plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.6.0</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals><goal>shade</goal></goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.example.App</mainClass>
          </transformer>
        </transformers>
      </configuration>
    </execution>
  </executions>
</plugin>

Check Maven Central for the latest plugin version. The Maven Assembly plugin with the jar-with-dependencies descriptor is a common alternative.

JARs Testers Run Every Day

  • Selenium Grid: java -jar selenium-server-<version>.jar standalone starts a Grid on port 4444.
  • WireMock standalone: run a mock API server from a single JAR.
  • JMeter and many test tools ship as JARs started by a script.
  • Your own test utilities: package a data generator or report tool as an executable JAR so teammates and Jenkins can run it without an IDE.

JAR vs WAR vs EAR

A JAR holds classes and resources for a library or standalone application. A WAR (Web ARchive) packages a web application for a servlet container such as Tomcat. An EAR bundles several modules for a Jakarta EE application server. Spring Boot applications are usually built as executable JARs with an embedded server.

FAQs

What is a JAR file in Java?

A ZIP-format archive containing compiled classes, resources and a manifest. Libraries are distributed as JARs, and a JAR with a Main-Class entry can be run with java -jar.

How do I run a JAR file?

java -jar file.jar if the manifest has a Main-Class, otherwise java -cp file.jar com.example.MainClass. Dependencies must also be on the classpath unless it's a fat JAR.

What does 'no main manifest attribute' mean?

The JAR's manifest has no Main-Class entry, so java -jar doesn't know which class to start. Rebuild it with jar cfe or configure the main class in Maven.

What is a fat JAR?

An executable JAR that includes all its dependencies, built with the Maven Shade or Assembly plugin, so it runs anywhere with just java -jar.