亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

maven項目錯誤:找不到或無法加載主類?XXX問題

 更新時間:2024年02月28日 16:53:48   作者:可樂還是甜的好  
這篇文章主要介紹了maven項目錯誤:找不到或無法加載主類?XXX問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

maven項目錯誤:找不到或無法加載主類 XXX

今天在接手一個項目的時候

運行main類報了這個錯 錯誤: 找不到或無法加載主類 XXX 經(jīng)過好一番查證才找出了問題所在

原因是

maven項目的<scope>provided</scope>導(dǎo)致的,現(xiàn)在記錄一下。

測試代碼

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>上,

經(jīng)查找官網(wǎng)得知,具有此范圍的依賴項會添加到用于編譯和測試的類路徑中,但不會添加到運行時類路徑中,交由JDK 或容器在運行時提供依賴項。

因此本地運行的時候就報了 找不到或無法加載主類,而放在服務(wù)器,因為容器在運行時提供依賴項,所以運行沒問題。

官網(wǎng)地址:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#

遇到問題要逐步分析,分解問題,鎖定問題根源;另外要多看官網(wǎng)文檔!

maven 項目常見錯誤解決

編譯錯誤

由于 jdk 編譯級別設(shè)定不匹配,導(dǎo)致代碼編譯錯誤。

idea 創(chuàng)建的 maven 項目,默認(rèn)使用 jdk1.5 的版本進(jìn)行編譯,會導(dǎo)致編譯失敗。

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 命令直接運行,有可能出現(xiàn) ***.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

通過對比錯誤與正常的配置清單文件,可以發(fā)現(xiàn),運行錯誤的清單文件中缺少 Main-Class 屬性參數(shù)配置,增加該屬性后,既可正常運行

附件: 通用 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>

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論