【问题标题】:Why does jar file that created by assembly:single cannot find main class ?为什么由 assembly:single 创建的 jar 文件找不到主类?
【发布时间】:2017-10-09 12:35:41
【问题描述】:

我有一个 Maven 项目。 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>BestOfferz</groupId>
    <artifactId>BestOfferz</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>3.4.1</version>
        </dependency>

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

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web-client</artifactId>
            <version>3.4.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.company.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>


    </build>

</project>

我通过命令创建一个jar文件:

mvn clean compile assembly:single

然后我尝试运行通过以下方式提取的 jar:

java -cp 目标/BestOfferz-1.0-SNAPSHOT-jar-with-dependencies.jar com.company.Main

我得到一个错误:

错误:无法找到或加载主类 com.company.Main

我的主类看起来是这样的:

包 com.company;

public class Main {
    ....
    public static void main(String[] args) throws FileNotFoundException {
       ....
    }
}

【问题讨论】:

    标签: java maven jar


    【解决方案1】:

    以下 pluginRepository 完成了这项工作:

    <pluginRepositories>
        <pluginRepository>
            <id>onejar-maven-plugin.googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
      </pluginRepositories>
    

    是项目节点的直接节点。

    感谢 Yair Zaslavsky。

    【讨论】:

      【解决方案2】:

      你可以说

           mvn install
      

           mvn clean install
      

      然后你的 jar 被构建并通过

           java -jar YourJar.jar
      

      【讨论】:

      【解决方案3】:


      编译是不够的,创建JAR的时候还需要“打包”,
      即调用 -

      mvn package
      

      请在此处阅读 - Build lifecyle

      【讨论】:

        猜你喜欢
        • 2014-12-14
        • 2019-04-22
        • 2017-12-10
        • 2018-06-29
        • 1970-01-01
        • 2020-02-28
        • 2016-09-29
        • 1970-01-01
        • 2012-01-03
        相关资源
        最近更新 更多