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

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

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

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

屏幕截图来自Idea:
java - Maven无法获得可执行jar:无法加载主类-LMLPHP

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


但是结果一样

最佳答案

问题是您已经在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;

关于java - Maven无法获得可执行jar:无法加载主类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57638143/

10-09 05:53