【问题标题】:Error: Could not find or load main class while executing a .jar file错误:执行 .jar 文件时无法找到或加载主类
【发布时间】:2020-06-02 14:16:11
【问题描述】:

我使用 Intellij 创建了一个 Dropwizard 应用程序。 (这里com.indore.GalaxyApp是我的MainClass)的名字

这是我的应用程序的pom.xml

使用mvn clean package 构建项目后,在我的target 目录中创建了一个jar 文件。

现在,我正在尝试通过终端运行带有 jar 文件的应用程序,我得到以下响应:

MANIFEST.MF

为了使jar 可执行

  1. 我已经配置了 Maven 存档器,并在 pom.xml 中添加了以下插件,以适应这个 SO answer

  2. 我什至通过以下方式运行应用程序:

    java -cp target/galaxy-1.0-SNAPSHOT.jar com.indore.GalaxyApp

但仍然出现同样的错误。

我参考了这些帖子:

setup-main-class-in-manifest, Cant execute jar file

谁能告诉我解决这个问题的方法?

【问题讨论】:

  • 该错误告诉您一个类 io.dropwizard.Application 是必需的,但没有找到,这听起来像是缺少一个额外的 jar,需要在应用程序运行时类路径上可用。清单是否有类路径行?
  • @Gimby 清单中没有类路径行
  • @Gimby 你能告诉我如何在清单中添加类路径行
  • 如果插件配置正确,Maven 应该会为您执行此操作。它有很好的记录。不过,您可能想使用 Maven Shade 插件创建一个胖 jar,那么您只需要担心一个(大)jar。
  • 关注 the documentation 到一个 T,包括 pom.xml。

标签: java maven jar dropwizard


【解决方案1】:

我可以通过关注documentation 来解决此问题。

这里是示例pom.xml 供参考

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/io.dropwizard/dropwizard-core -->
    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>2.0.25</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.dropwizard/dropwizard-db -->
    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-db</artifactId>
        <version>2.0.25</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.dropwizard/dropwizard-hibernate -->
    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-hibernate</artifactId>
        <version>2.0.13</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.3.1</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.example.helloworld.HelloWorldApplication</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

【讨论】:

    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2015-07-27
    • 2014-09-04
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多