java Maven无法获取可执行jar:无法加载主类
你是我最后的希望。我为电报写了一个小机器人。它的工作原理非常完美,但我无法将其构建为可执行的jar文件。每次我试图在服务器上执行jar文件时,都会出现错误:
Error: Could not find or load main class com.praetorian19.MainIni
我知道我提到的课堂路径是错误的。我试着用不同的方式提到它,但这并不重要。错误是一样的。请查看我的MVN项目xml和Idea项目结构中的几个屏幕截图。我认为我在pom或结构上有错误
我尝试用多种方式编写类路径。但结果是一样的 我尝试在两个平台上构建jar——Win/Lin,同样如此
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。总督府19。缅因 当我试图在服务器上执行jar时
我也试着
总督府19。缅因
但结果是一样的
# 1 楼答案
问题是您在
maven-assembly-plugin
中将主类定义为com.praetorian19.MainInit
,这意味着MainInit
类应该在包com.praetorian19
中然而,当查看IDEA项目结构时,您的MainInit类在默认包中,因此在执行
jar
时,它会给出错误Could not find or load main class com.praetorian19.MainIni
要解决这个问题,请创建一个包
com.praetorian19
,并将MainInit
类移动到该package
中,包声明为package com.praetorian19;