gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:22:18 25 4
gpt4 key购买 nike

我使用 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 hereMANIFEST.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com