我使用 Intellij 创建了一个 Dropwizard 应用程序。
(这里com.indore.GalaxyApp
是我的MainClass的名称)
这是pom.xml我的应用程序。
使用mvn clean package
构建项目后,在我的target
目录中创建了一个jar
文件。
现在,我尝试通过终端运行包含 jar 文件的应用程序,我得到以下响应:
为了使jar
可执行
我已经配置了 Maven Archiver 并按照此 SO answer 在
pom.xml
中添加了以下插件我什至通过以下方式运行应用程序:
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/