【问题标题】:How do I specify a main class in the manifest of my generated jar file? [duplicate]如何在生成的 jar 文件的清单中指定主类? [复制]
【发布时间】:2016-10-06 21:25:09
【问题描述】:

我尝试使用 Eclipse Maven 构建具有依赖项的 jar 但是当我执行

java -jar App.jar

得到错误:“找不到主要方法”

我的 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>com.maventest</groupId>
    <artifactId>aproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>aproject</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>

这只是一个简单的java代码:

package com.maventest.aproject;

public class App {
    public static void main( String[] args ) {
        System.out.println( "Hello World!" );
    }
}

eclipse 版本:4.5.2

谁能帮我解决这个问题? 谢谢。

【问题讨论】:

    标签: java maven


    【解决方案1】:

    您可以为此目的使用 maven-jar-plugin。

    <build>
        <plugins>
             <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${maven-jar-plugin.version}</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- <addClasspath>true</addClasspath> -->
                            <mainClass>twaren.elk.elkMaven.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
          </plugins>
    </build>
    

    【讨论】:

    猜你喜欢
    • 2011-01-06
    • 2016-05-14
    • 2023-03-18
    • 2011-02-20
    • 2013-02-20
    • 2018-04-10
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    相关资源
    最近更新 更多