How do I specify a main class in the manifest of my generated jar file?
Author: Deron Eriksson
Description: This tutorial describes how to specify a main class in the MANIFEST.MF file that gets automatically generated for a maven jar project.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


In JavaSW, an executable jarW file specifies its main class in the MANIFEST.MF file in that jar file. For a mavenSW project that features "jar" packaging, we can specify the main class for the MANIFEST.MF file by specifying it in our pom.xml, as shown here:

pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.maventest</groupId>
	<artifactId>aproject</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>aproject</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.maventest.App</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

This specifies that the com.maventest.App class in the project is the main class that should be executed if someone attempts to execute the jar file. Additional options that can be configured for the Maven Archiver can be found at http://maven.apache.org/shared/maven-archiver/index.html.

If we perform a "mvn clean package" to build the project's jar file and then examine the MANIFEST.MF file that is generated in the jar file at META-INF/MANIFEST.MF, we see that it contains the following:

MANIFEST.MF file in generated jar file

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Cakes
Build-Jdk: 1.6.0_04
Main-Class: com.maventest.App

I'll open a command prompt and go to the directory containing the jar file. If I attempt to execute the generated jar file via "java -jar aproject-1.0-SNAPSHOT.jar", we see the "Hello World!" message that is output to the console as a result of the execution of com.maventest.App.

java -jar aproject-1.0-SNAPSHOT.jar