Maven无法获得可执行jar:无法加载主类

时间:2019-08-24 12:54:26

标签: java maven jar executable-jar

您是我的最后希望。我写了小机器人电报。它的工作原理完美,但我无法将其构建为可执行的jar文件。每次我尝试在服务器上执行jar文件时,都会收到错误消息:

Error: Could not find or load main class com.praetorian19.MainIni

我知道我以错误的方式提到了课堂路径。我尝试以不同的方式提及它,但这没关系。错误是一样的。请查看我的MVN项目xml和Idea项目结构的一些屏幕截图。我认为我在pom或结构上有错误。

我尝试以多种方式编写类路径。但是结果一样 我尝试在两个平台上都构建jar-Win / Lin,相同

想法的屏幕截图: Project structure

Pom:

<modelVersion>4.0.0</modelVersion>
    <groupId>praetorian19</groupId>
    <artifactId>praetorian19</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>4.4.0.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.12.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.12.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-jcl -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>2.12.1</version>
        </dependency>

    </dependencies>


    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>

            <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.praetorian19.MainInit
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

错误:找不到或加载主类com.praetorian19.MainInit 当我尝试在服务器上执行jar时

我也尝试

praetorian19.MainInit

但结果相同

1 个答案:

答案 0 :(得分:0)

问题是您在com.praetorian19.MainInit中将主类定义为maven-assembly-plugin,这意味着MainInit类应该在软件包com.praetorian19中。

但是,当查看IDEA项目结构时,您的MainInit类位于默认包中,因此在执行Could not find or load main class com.praetorian19.MainIni时会给出错误jar

要解决此问题,请创建一个包com.praetorian19,并将您的MainInit类移动到该package中,并将包声明声明为package com.praetorian19;