繁体   English   中英

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

[英]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>

错误

错误:找不到或加载主类com.example.Authentication,原因:java.lang.NoClassDefFoundError:io / grpc / BindableService

内容:

我正在通过IntelliJ-> Jar Application Run配置运行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");
    }

}

编辑

PS我解压缩了.jar文件,可以确认我可以在其中看到Authentication.class文件,也许它必须与grpc一起执行某些操作,无法找到该类文件。

您可以尝试从命令提示符运行jar吗?

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

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

Maven组装插件

该插件将所有依赖项添加到最终的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阴影插件

该插件将所有依赖项添加到最终的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM