我有一个Swing应用程序,我想将JavaFx集成到它中。当从Eclipse启动时,应用程序运行良好。
下面是主类:
package org.openjfx.hellofx;
import java.awt.BorderLayout;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;
public class AppRun {
public static void main(String[] args) {
JFrame f = new JFrame("TEST");
f.setSize(400, 400);;
f.getContentPane().setLayout(new BorderLayout());
JFXPanel panel = new JFXPanel();
Scene s = new Scene(new javafx.scene.control.Label("JavaFx"));
panel.setScene(s);
f.getContentPane().add(panel);
f.setVisible(true);
}
}
必要的依赖关系在Maven 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openjfx</groupId>
<artifactId>hellofx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<javafx.version>16</javafx.version>
<javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>${javafx.version}</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>
<configuration>
<mainClass>org.openjfx.hellofx.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjfx.hellofx.AppRun</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
但当我构建并启动一个Fat Jar时,它不起作用:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: jdk/swing/interop/SwingInterOpUtils
at com.sun.javafx.embed.swing.newimpl.JFXPanelInteropN.isUngrabEvent(JFXPanelInteropN.java:40)
at javafx.embed.swing.JFXPanel.lambda$new$6(JFXPanel.java:834)
at java.desktop/java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source)
at java.desktop/java.awt.Toolkit.notifyAWTEventListeners(Unknown Source)
at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)
缺少的文件'SwingInterOpUtils‘在模块'jdk.unsupported.dekstop’中。而且这不是标准JRE的一部分。那么,有没有可能使用Maven构建来运行应用程序呢?
发布于 2021-08-26 09:19:57
我不知道您是否正在创建一个模块化的应用程序,但我认为您应该考虑使用javafx-maven-plugin
而不是maven-shade-plugin
来生成您的应用程序的自定义本机映像。您已经包含了插件,只需运行mvn clean javafx:jlink
即可。查看插件的GitHub自述文件以了解配置详细信息:https://github.com/openjfx/javafx-maven-plugin
https://stackoverflow.com/questions/68935160
复制相似问题