简体   繁体   中英

How to set up the main class header in the manifest

I've created a simple project in Java Maven . I can run this project by:

1:

java -cp target/MavenTestApp-1.0-SNAPSHOT.jar org.koushik.javabrains.App

But I would like to run it by:

2:

java -jar target/MavenTestApp-1.0-SNAPSHOT.jar

To run it by second method I need to change manifest file . I am using maven so I think that in this case I need to change the pom.xml file. My pom.xml file is:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.koushik.javabrains</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenTestApp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

I need to add something like this:

<mainClass>org.koushik.javabrains.App</mainClass>

But I not pretty sure where and how to do that? Could you also tell me if this line is good:

2:

java -jar target/MavenTestApp-1.0-SNAPSHOT.jar

Maybe at the end of this line there should be something or it is sufficient? Thanks.

Take a look at the Maven jar plugin documentation . You will need to configure something similar in your .pom:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>org.koushik.javabrains.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM