maven-jar-plugin mainClass - 无法找到或加载 main class
maven-jar-plugin mainClass - Could not find or load main class
我在我的 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");
}
}
编辑
P.S。我解压缩了 .jar 文件并可以确认我可以在那里看到 Authentication.class 文件,也许它必须对无法找到 class 文件的 grpc 执行某些操作。
你能尝试从命令提示符 运行 jar 吗?
maven-jar-plugin 您使用的是一个非常基本的插件,可以生成 JAR,但它不会在最终 JAR 中添加 Maven 依赖项。
要创建可执行的 fat JAR,请考虑使用以下插件之一:
此插件将所有依赖项添加到最终 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>
此插件将所有依赖项添加到最终 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>
我在我的 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");
}
}
编辑
P.S。我解压缩了 .jar 文件并可以确认我可以在那里看到 Authentication.class 文件,也许它必须对无法找到 class 文件的 grpc 执行某些操作。
你能尝试从命令提示符 运行 jar 吗?
maven-jar-plugin 您使用的是一个非常基本的插件,可以生成 JAR,但它不会在最终 JAR 中添加 Maven 依赖项。
要创建可执行的 fat JAR,请考虑使用以下插件之一:
此插件将所有依赖项添加到最终 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>
此插件将所有依赖项添加到最终 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>