java - jar 文件错误 : Could not find or load main class while executing a .

标签 java maven jar dropwizard

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

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

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

enter image description here

enter image description here

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

enter image description here

enter image description here MANIFEST.MF

enter image description here

为了使jar可执行

  1. 我已经配置了 Maven Archiver 并按照此 SO answerpom.xml 中添加了以下插件

    enter image description here

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

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

但仍然遇到同样的错误。

我引用了这些帖子:

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

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

最佳答案

我能够按照 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>

关于java - jar 文件错误 : Could not find or load main class while executing a .,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60282934/

相关文章:

java - 为什么有时需要将 JAR 添加到构建路径而有时不需要?

java - equals() 方法中使用的字符串类值变量 : how and when is it defined?

Maven:在pom.xml中设置settings.xml位置?

eclipse - Eclipse 和 Maven 多模块项目的依赖性问题

java - 在应用程序中使用多个版本的 jar 会导致问题吗?

java - 如果 jar 文件大于特定大小,Applet 无法在 Windows 7 上使用 Java 1.7 加载

java - JSP 表达式语言和动态属性名称

java - Groovy DSL 与 Java

java - 将变量传递给 findViewByID

java - Maven许可证插件: Unknown license