gpt4 book ai didi

java - maven-jar-plugin mainClass - 无法找到或加载主类

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

我在我的 Maven 中指定了一个插件来使用 maven-jar-plugin 构建 jar。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.Authentication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

错误

Error: Could not find or load main class com.example.Authentication Caused by: java.lang.NoClassDefFoundError: io/grpc/BindableService

上下文:

我通过 IntelliJ -> Jar 应用程序运行配置运行 jar 文件,没有 VM 选项或传递任何程序参数。

public class Authentication extends AuthenticationGrpc.AuthenticationImplBase {

public static void main(String[] args) throws Exception {
Server server = ServerBuilder.forPort(8080)
.addService(new Authentication())
.build();

server.start();
server.awaitTermination();
System.out.println("Server listening at 8080");
}

}
<小时/>

编辑

附注我解压了 .jar 文件,并可以确认我可以在那里看到 Authentication.class 文件,也许它必须与 grpc 无法找到类文件有关。

最佳答案

<强> maven-jar-plugin 您使用的是一个非常基本的插件,可以生成 JAR,但它不会在最终 JAR 中添加 Maven 依赖项。

要创建可执行 fat JAR,请考虑使用以下插件之一:

<强> maven-assembly-plugin

此插件将所有依赖项添加到最终 JAR 中。

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

maven-shade-plugin

该插件将所有依赖项添加到最终 JAR 中并执行着色(即重命名)

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Authentication</mainClass>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

关于java - maven-jar-plugin mainClass - 无法找到或加载主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57447228/

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