gpt4 book ai didi

java - Maven 错误 : Could not find or load main class (pom. xml 文件)

转载 作者:行者123 更新时间:2023-12-02 11:16:04 27 4
gpt4 key购买 nike

我正在努力执行我正在创建的 jar 文件。这是我的 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.mycompany</groupId>
<artifactId>mavenproject3</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.mavenproject3.SSBStub</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.17.0</version>
</dependency>
</dependencies>
</project>

当我尝试在命令提示符下运行此 jar 文件时,出现错误“无法找到或加载主类”。我已经尝试了一切,但似乎无法解决。有人可以帮我解决这个问题吗?

另外,有人可以告诉我在哪里也可以找到 list 文件吗?我不知道在 netbeans 中哪里可以找到它。

谢谢

最佳答案

如果您尝试使用命令提示符运行它,您会希望所有依赖项都存在于您的 jar 本身中。

您可以使用 maven- assembly-plugin 来实现此目的。使用下面的代码来实现这一点。

       <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.mycompany.mavenproject3.SSBStub</mainClass>
</manifest>
</archive>

</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

使用 Maven 目标运行:mvn clean package

这应该在您的目标目录中创建两个 jar。一种没有依赖项,一种具有所有依赖项。运行名为:myJar-jar-with-dependency.jar 的 jar

关于java - Maven 错误 : Could not find or load main class (pom. xml 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50281398/

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