maven项目错误:找不到或无法加载主类 XXX问题
作者:可乐还是甜的好
maven项目错误:找不到或无法加载主类 XXX
今天在接手一个项目的时候
运行main类报了这个错 错误: 找不到或无法加载主类 XXX 经过好一番查证才找出了问题所在
原因是
maven项目的<scope>provided</scope>导致的,现在记录一下。
测试代码
import org.apache.flink.table.functions.ScalarFunction;
public class Test extends ScalarFunction {
public static void main(String[] args) {
System.out.println("test");
}
}
该类继承了flink的ScalarFunction,但是maven的pom.xml文件是这么写的依赖;
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner_2.11</artifactId>
<version>1.8.3</version>
<scope>provided</scope>
</dependency>
问题就出在了<scope>provided</scope>上,
经查找官网得知,具有此范围的依赖项会添加到用于编译和测试的类路径中,但不会添加到运行时类路径中,交由JDK 或容器在运行时提供依赖项。
因此本地运行的时候就报了 找不到或无法加载主类,而放在服务器,因为容器在运行时提供依赖项,所以运行没问题。
官网地址:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#
遇到问题要逐步分析,分解问题,锁定问题根源;另外要多看官网文档!
maven 项目常见错误解决
编译错误
由于 jdk 编译级别设定不匹配,导致代码编译错误。
idea 创建的 maven 项目,默认使用 jdk1.5 的版本进行编译,会导致编译失败。
Error:java: Compilation failed: internal java compiler error


解决办法:
修改 pom.xml 文件,增加 maven 编译插件配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
jar中没有主清单属性
maven 编译插件编译后生成的 jar,如果通过 java -jar 命令直接运行,有可能出现 ***.jar 中没有主清单属性
解决办法:
使用 maven-shade-plugin 打包插件,配置上 main class 位置
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<minimizeJar>false</minimizeJar>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
运行出错的配置清单文件 META-INF/MANIFEST.MF
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: dataojo Created-By: Apache Maven 3.5.4 Build-Jdk: 1.8.0_131
运行正常的配置清单文件 META-INF/MANIFEST.MF
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: dataojo Created-By: Apache Maven 3.5.4 Build-Jdk: 1.8.0_131 Main-Class: Application
通过对比错误与正常的配置清单文件,可以发现,运行错误的清单文件中缺少 Main-Class 属性参数配置,增加该属性后,既可正常运行
附件: 通用 mvn 打包配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<minimizeJar>false</minimizeJar>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
