【问题标题】:Eclipse and Maven: Error occured during initialization of boot layerEclipse 和 Maven:启动层初始化期间发生错误
【发布时间】:2021-10-03 05:46:51
【问题描述】:

邦焦尔诺,

我有一个带有一些额外依赖项的 Java 项目,我想让 Maven 处理它。

所以我在 pom.xml 中添加了所有依赖项并使用 Maven 更新了项目。在我的 Maven 依赖项中,我拥有运行程序所需的每个包,但是我无法再次运行我的项目,因为我收到了这个错误:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.base not found

我是否仍然需要在我的 VM 参数(运行配置)中添加模块,还是不需要它们,因为它们已添加到 throw maven?

如果我在没有任何 VM 参数的情况下运行它,我会得到:

Error: JavaFX runtime components are missing, and are required to run this application

这是 Maven 依赖项的屏幕截图:

编辑:我能够使构建工作,但如果我想运行创建的 .JAR 文件,它会给我这个错误:

Error: JavaFX runtime components are missing, and are required to run this application 

我的 pom.xml:

<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>kantinen_managementsystem</groupId>
  <artifactId>kantinen_managementsystem</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Test KMS Maven</name>
  <description>Test Maven Build</description>
  
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>16</maven.compiler.source>
    <maven.compiler.target>16</maven.compiler.target>
        <java.version>16</java.version>
    </properties>  
  
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>images</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
    
  <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
                <addClasspath>true</addClasspath>
              <mainClass>KMS.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>    
    
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <archive>
            <manifest>
                <addClasspath>true</addClasspath>
            <mainClass>KMS.Main</mainClass>
            </manifest>
            </archive>
          <release>16</release>
        </configuration>
      </plugin>
      
    <plugin>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>0.0.6</version>
        <configuration>
            <mainClass>KMS.Main</mainClass>
        </configuration>
    </plugin>    
    
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
            <archive>
            <manifest>
                <addClasspath>true</addClasspath>
            <mainClass>KMS.Main</mainClass>
            </manifest>
            </archive>        
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin> 
      
    </plugins>
  </build>
  
  <dependencies>
    <dependency>                                                    <!-- PostgreSQL -->
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.20</version>
    </dependency>
    
    <dependency>                                                    <!-- AnimateFX -->
        <groupId>io.github.typhon0</groupId>
        <artifactId>AnimateFX</artifactId>
        <version>1.2.1</version>
    </dependency>
    
    <dependency>
        <groupId>de.jensd</groupId>
        <artifactId>fontawesomefx</artifactId>
        <version>8.2</version>
    </dependency>
    
    <dependency>                                                    <!-- JavaFX - Controls -->
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>16</version>
    </dependency>
    
    <dependency>                                                    <!-- JavaFX - Graphics -->
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics</artifactId>
        <version>16</version>
    </dependency>
    
    <dependency>                                                    <!-- JavaFX - Base -->
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-base</artifactId>
        <version>16</version>
    </dependency>
    
    <dependency>                                                    <!-- JavaFX - FXML -->
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>16</version>
    </dependency>
    
    <dependency>                                                    <!-- JavaFX - Web -->
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>16</version>
    </dependency>
    

<dependency>                                                    <!-- JavaFX - Swing -->
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-swing</artifactId>
    <version>16</version>
</dependency>

<dependency>                                                    <!-- JavaFX - Media -->
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-media</artifactId>
    <version>16</version>
</dependency>
                                                              

【问题讨论】:

  • 查看官方文档:openjfx.io/openjfx-docs/#maven 您可以使用 javafx 插件,也可以手动处理模块内容。
  • @mipa 所以我调查了一下,谢谢。但是我仍然在运行该项目时遇到问题。即使我像文档中所说的那样添加了 javafx-maven-plugin,它也只是找不到 javafx 模块。
  • 你的 pom.xml 看起来怎么样?
  • 您检查过这些文件javafx-base-16.jarjavafx-base-16-win.jar是否有效?您可以通过在文本编辑器中打开这些文件来完成。内容必须以字母PK开头
  • 看起来您正在尝试为您的应用程序创建一个胖 jar 文件。 openjfx documentation 提供了 sample project 来实现这一点,它利用了 org.openjfx javafx-maven-plugin 和 maven-shade-plugin。我建议您尝试使示例项目在您的环境中运行,然后如果您可以使其正常工作,请将示例中使用的相同原则应用于您的项目,以使您的项目按您的意愿运行。

标签: java eclipse maven javafx


【解决方案1】:

这里是问题的解释 - 以及如何解决它:

原因是 Java 运行时会检查主类是否扩展了 javafx.application.Application,如果是这样,它强烈要求 javafx 平台可以作为模块使用,而不是作为 jar 包.

要修复它,您必须创建一个“普通”Main 并在其中链接您的 FX Main,之后您必须在 pom.xml 设置中选择新的 Main。

工作示例: src/Main.java

public class Main {

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

src/HelloFX.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
//import javafx.graphics;

public class HelloFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        Scene scene = new Scene(l, 640, 480);
        stage.setScene(scene);
        stage.show();
    }

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

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>HelloFX</groupId>
  <artifactId>HelloFX</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>10</release>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
                <classifier>win</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
                <classifier>linux</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
                <classifier>mac</classifier>
    </dependency>
  </dependencies>
</project>

信用:https://github.com/javafxports/openjdk-jfx/issues/236

【讨论】:

    【解决方案2】:

    您可以将 javafx 文件移动到其他位置吗?例如 c:/ 有时此类错误可能与文件位置有关。 复制文件后重新定义。 您可以打开一个不同的项目并尝试。 我的回答可能不好,但我想给出一个想法。

    【讨论】:

      猜你喜欢
      • 2019-02-24
      • 2021-05-17
      • 1970-01-01
      • 2021-03-02
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      相关资源
      最近更新 更多