gpt4 book ai didi

java - Maven 创建不正确的 list 文件,jar 文件不可执行,因为未指定主类

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

我正在尝试生成一个可以通过命令提示符执行的 .jar 文件。我在这个项目中使用 Maven,以便我可以使用 Unirest(通过房地产经纪人 API 获取数据)。然而,首先,我尝试将一个简单的“hello world”程序编译为可执行的 .jar 文件,并且 Maven 没有将 main-class 属性添加到 Manifest 文件中。我使用了this website上的说明在“Maven Assembly Plugin”部分下。这是 pom.xml 的相关部分:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.javaRealtor.app.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

.java 文件位于 java-realtor\src\main\java\com\javaRealtor\app\App.java。我需要以不同的方式指定插件的 MainClass 部分吗?我也尝试仅使用 com.javaRealtor.App ,但这不起作用。 list 文件(我提取了 jar 来查看它,这样我也许可以弄清楚发生了什么)仍然没有主类的行。

最佳答案

您需要使用阴影插件来进行此转换

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.javaRealtor.app.App</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

请注意:请避免在包装名称中使用驼峰式大小写,以便:

  • 避免管理多个开发平台时出现问题(区分大小写:linux/unix 和不区分大小写的 Windows)
  • Java Conventions 投诉

关于java - Maven 创建不正确的 list 文件,jar 文件不可执行,因为未指定主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62225262/

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