如何将 JavaFX 项目正确转换为 Maven?
How to convert JavaFX project to Maven correctly?
我在 Eclipse 中编写了 JavaFx 项目 IDE,它工作正常。
将其转换为 Maven 项目后,我的 FMXL 文件停止打开。但是,如果我创建新的 FXML 文件并将它们放在相同的文件夹中,一切正常。
有什么方法可以 运行 我的 Maven 项目而不用重新创建我所有的 FXML 文件吗?
我已经尝试过在代码中更改我的 FXML 文件的路径,将 FXML 移动到 src/main/resources 包或只是将代码从旧的 FXML 复制到新的 FXML,但没有任何效果。
我在 SceneBuilder 中创建的所有 FXML,并且在整个过程中没有更改此程序的版本。
我正在使用以下方法加载我的 FXML:
private void showMainPage() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/MainView.fxml"));
mainLayout = loader.load();
Scene scene = new Scene(mainLayout);
primaryStage.setScene(scene);
primaryStage.show();
}
我的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>fx</groupId>
<artifactId>maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fxMaven</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>ru.jfxreselection.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</build>
我尝试 运行 使用旧的 .FXML 文件时遇到的错误
Exception in Application start method
java.lang.reflect.InvocationTargetException at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567) at
javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567) at
java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start
method at
javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:831) Caused by:
javafx.fxml.LoadException:
/C:/Users/Tormo/eclipse-workspace/rslFxNewMaven/target/classes/reselection/view/MainView.fxml:48
at
javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516) at
rslFxNewMaven/reselection.Main.showMainPage(Main.java:28) at
rslFxNewMaven/reselection.Main.start(Main.java:22) at
javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:847)
at
javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:484)
at
javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:457)
at
java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at
javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:456)
at
javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native
Method) at
javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:184)
... 1 more Caused by: java.lang.reflect.InaccessibleObjectException:
Unable to make private void
reselection.view.MainViewController.goDacSearch() throws
java.io.IOException accessible: module rslFxNewMaven does not "opens
reselection.view" to module javafx.fxml at
java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
at
java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at
java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at
javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleMethods(FXMLLoader.java:3611)
at
javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.run(FXMLLoader.java:3543)
at
javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.run(FXMLLoader.java:3536)
at
java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
at
javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleMembers(FXMLLoader.java:3535)
at
javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.getControllerMethods(FXMLLoader.java:3494)
at
javafx.fxml/javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:573)
at
javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at
javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:781)
at
javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
... 13 more Exception running application reselection.Main
这与 Maven 无关,这是由于您的应用程序 Java 模块系统配置不正确导致的访问错误。
错误信息告诉你哪里出了问题:
module rslFxNewMaven does not "opens reselection.view" to module javafx.fxml
学习 -> java modules.
将以下行添加到您的模块中-info.java:
opens reselection.view to javafx.fxml;
我在 Eclipse 中编写了 JavaFx 项目 IDE,它工作正常。
将其转换为 Maven 项目后,我的 FMXL 文件停止打开。但是,如果我创建新的 FXML 文件并将它们放在相同的文件夹中,一切正常。
有什么方法可以 运行 我的 Maven 项目而不用重新创建我所有的 FXML 文件吗?
我已经尝试过在代码中更改我的 FXML 文件的路径,将 FXML 移动到 src/main/resources 包或只是将代码从旧的 FXML 复制到新的 FXML,但没有任何效果。
我在 SceneBuilder 中创建的所有 FXML,并且在整个过程中没有更改此程序的版本。
我正在使用以下方法加载我的 FXML:
private void showMainPage() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/MainView.fxml"));
mainLayout = loader.load();
Scene scene = new Scene(mainLayout);
primaryStage.setScene(scene);
primaryStage.show();
}
我的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>fx</groupId>
<artifactId>maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fxMaven</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>ru.jfxreselection.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</build>
我尝试 运行 使用旧的 .FXML 文件时遇到的错误
Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071) Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:196) at java.base/java.lang.Thread.run(Thread.java:831) Caused by: javafx.fxml.LoadException: /C:/Users/Tormo/eclipse-workspace/rslFxNewMaven/target/classes/reselection/view/MainView.fxml:48 at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516) at rslFxNewMaven/reselection.Main.showMainPage(Main.java:28) at rslFxNewMaven/reselection.Main.start(Main.java:22) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:847) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:484) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:457) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:456) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:184) ... 1 more Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make private void reselection.view.MainViewController.goDacSearch() throws java.io.IOException accessible: module rslFxNewMaven does not "opens reselection.view" to module javafx.fxml at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleMethods(FXMLLoader.java:3611) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.run(FXMLLoader.java:3543) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.run(FXMLLoader.java:3536) at java.base/java.security.AccessController.doPrivileged(AccessController.java:312) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleMembers(FXMLLoader.java:3535) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.getControllerMethods(FXMLLoader.java:3494) at javafx.fxml/javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:573) at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610) at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:781) at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
... 13 more Exception running application reselection.Main
这与 Maven 无关,这是由于您的应用程序 Java 模块系统配置不正确导致的访问错误。
错误信息告诉你哪里出了问题:
module rslFxNewMaven does not "opens reselection.view" to module javafx.fxml
学习 -> java modules.
将以下行添加到您的模块中-info.java:
opens reselection.view to javafx.fxml;