【问题标题】:Error when trying to make a fat jar with maven in JavaFX on Intellij尝试在 Intellij 上的 JavaFX 中使用 maven 制作胖 jar 时出错
【发布时间】:2022-01-01 21:18:00
【问题描述】:

我一直在尝试按照本教程为我的 JavaFX 项目创建一个带有 maven 和 maven-shade-plugin 的 jar。 https://www.youtube.com/watch?v=EyYb0GmtEX4.

我可以创建 jar 文件,但是当我运行它时,它给了我错误: Error: Could not find or load main class com.example.pleasework.com.example.pleasework.Main_1

Caused by: java.lang.ClassNotFoundException: com.example.pleasework.com.example.pleasework.Main_1

我已经尝试添加 java --module-path /path/to/java-fx-libs/ --add-modules javafx.controls,javafx.fxml MyMainClass 到 vm 选项,但它只是将错误更改为 Error: Could not find or load main class Java。另外,我还有第二个主文件,它运行我的第一个扩展应用程序的主文件。

我正在使用 IntelliJ,并通过 IntelliJ 的内置 JavaFX 项目和 Maven 创建了我的项目。我可以正常运行我的项目并且工作正常,但是当我尝试运行通过 Maven 的阴影插件生成的阴影 jar 时,我得到了这些错误。

这是我的项目结构:

这是我的第二个主文件

package com.example.pleasework;

public class Main_1 {
    public static void main(String[] args) {
        HelloApplication.main(args);
    }
}

这是我的第一个主文件

package com.example.pleasework;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>PleaseWork</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>PleaseWork</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.8.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.0.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.5.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.example.pleasework/com.example.pleasework.Main_1</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <configuration>
                            <compilerArgument>-proc:none</compilerArgument>
                            <includes>
                                <include>fun/n/learn/annotation/LogMeCustomAnnotationProcessor.java</include>
                                <!--include dependencies required for LogMeCustomAnnotationProcessor -->
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <transformers>
                                <transformer implementation=
                                                     "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.example.pleasework/com.example.pleasework.Main_1</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • com.example.pleasework.com.example.pleasework.Main_1 看起来很可疑

标签: java maven intellij-idea javafx jar


【解决方案1】:

错误信息表明您必须指定没有模块名称的主类。只需在那里使用完全限定的类名。完全限定从不包含斜杠,因为它不是路径名。因此,在您的情况下,完全限定的类名包括周围的 XML 标记(主类在您的 pom.xml 中使用了两次,并且在两种情况下都必须更正):

<mainClass>com.example.pleasework.Main_1</mainClass>

您可以自己构建完全限定的类名:使用要引用的类的 package 声明中的字符串,添加一个点,然后写入类声明中的类名,瞧,您就拥有了完全限定的类名类名。

【讨论】:

  • 看起来用户在模块名称前加上这是一个模块化应用程序。也许?我个人认为胖罐子是一种反模式,所以无论如何我都不会这样做。如果要捆绑应用程序,请使用 jpackage。
  • openjfx maven plugin documentation。该文档演示了一个 mainClass,模块名称后面带有 / ,后跟包和类名。但是,shade 插件会破坏模块化,这是不使用它的原因之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 2016-08-04
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
相关资源
最近更新 更多