Tuesday, March 13, 2012

Maven Jar Plugin: Setting Main-Class and Class-Path in Manifest

To start a Java application you have to tell JVM where is the program's entry point (Main Class) and where to search for classes (Class Path). Main class must have a public static method called main, which takes command arguments and does the initialization. If your Java application is packaged into JAR you can define Main-Class and Class-Path in manifest file, so that JVM would be able to find your program's entry point and load necessary classes. To do so you can use maven-jar-plugin that generates manifest with Main-Class and Class-Path parameters.

Add the following lines into your pom.xml:


Definition of the parameters:
  • addClasspath - generates Class Path from your dependencies
  • mainClass - name of the main class including package
  • manifestEntries / Class-Path - adds extra places to be added to Class Path

Resulting manifest file:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: sergej.sizov
Build-Jdk: 1.5.0_22
Main-Class: com.blogspot.vozis.MyApp
Class-Path: ../conf/ xmlrpc-2.0.jar commons-httpclient-3.0.jar log4j-1.2.15.jar 

To start the application you no more need to define Main Class or Class Path using -cp parameter.

Just run  java -jar MyApp.jar

No comments:

Post a Comment