简体   繁体   English

如何在清单中设置主类头

[英]How to set up the main class header in the manifest

I've created a simple project in Java Maven . 我已经在Java Maven中创建了一个简单的项目。 I can run this project by: 我可以通过以下方式运行该项目:

1: 1:

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

But I would like to run it by: 但我想通过以下方式运行它:

2: 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. 我正在使用Maven,因此我认为在这种情况下,我需要更改pom.xml文件。 My pom.xml file is: 我的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/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: 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 . 看一下Maven jar插件文档 You will need to configure something similar in your .pom: 您将需要在.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在清单文件中为Java设置主类和类路径 - how to set main-class and class-path in manifest file for java 问:没有主清单属性,并且无法找到或加载主 class。 如何构建 Gradle 的 Main Manifest 属性? - Q: No main manifest attribute, and Could not find or load main class. How do I structure the Main Manifest attribute for Gradle? 如何创建没有清单文件和主类的jar? - How to create a jar without manifest file and main class? 如何在 NetBeans 项目生成的 jar 中的清单文件中设置 Main 类 - How to setup Main class in manifest file in jar produced by NetBeans project 如何解决错误“无法从…加载Main-Class清单” - How to fix error “Failed to load Main-Class manifest from …” 如何在Eclipse中设置Run Configuration和Debug Configuration以选择将main方法作为启动类的类? - How to set Run Configuration and Debug Configuration in Eclipse to choose the class that has the main method as the start-up class? 如何在JarSplice中设置主类? - How to set the main class in JarSplice? 从mainClassName用gradle设置Main-Class标头 - set Main-Class header with gradle from mainClassName 如何通过jar设置主类可执行的vanilla groovy项目? - How to set up a vanilla groovy project with main class executable via jar? 如何使用Eclipse来体现Main? - How to manifest Main using Eclipse?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM