gpt4 book ai didi

java - 在 OpenJFX 中使用 SwingNode 出现不满意的链接错误

转载 作者:行者123 更新时间:2023-11-30 05:31:09 28 4
gpt4 key购买 nike

SwingNode 在使用 Spring Boot、OpenJFX 和 Maven 的应用程序中导致链接错误

我通读了有关此问题的帖子,尝试按照他们的建议进行操作(请参阅下面的代码),但无法解决我的问题。

有人可以帮忙吗?我哪里出错了?

我使用我找到的 Maven 示例在 IntelliJ 中运行它。

演示应用程序

package com.javafxswingexplore.demo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication extends Application {

private ConfigurableApplicationContext springContext;
private FXMLLoader fxmlLoader;

@Override
public void init() throws Exception {

System.setProperty("java.awt.headless", "false");

springContext = SpringApplication.run(DemoApplication.class);
fxmlLoader = new FXMLLoader();
fxmlLoader.setControllerFactory(springContext::getBean);
}

@Override
public void start(Stage stage) throws Exception {
fxmlLoader.setLocation(getClass().getResource("/primary.fxml"));

Parent root = fxmlLoader.load();
Scene scene = new Scene(root);

stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}

@Override
public void stop() {
springContext.stop();
}

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

FXMLController

package com.javafxswingexplore.demo;

import javafx.embed.swing.SwingNode;
import javafx.fxml.FXML;
import org.springframework.stereotype.Controller;

import javax.swing.*;

@Controller
public class FXMLController {

@FXML
SwingNode swingNode;

public void initialize() {

createAndSetSwingContent(swingNode);
}

private void createAndSetSwingContent(final SwingNode swingNode1) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode1.setContent(new JButton("Click me!"));
}
});
}
}

模块信息

module com.javafxswingexplore.demo {
requires javafx.controls;
requires javafx.fxml;
requires javafx.swing;

requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;

opens com.javafxswingexplore.demo to javafx.fxml, spring.core;
exports com.javafxswingexplore.demo;
}

pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javafxswingexplore</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>12</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>12.0.2</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>12.0.2</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12.0.2</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>12.0.2</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>12.0.2</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>com.javafxswingexplore.demo.DemoApplication</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

这是 Intellij 中的控制台输出:

"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" \
--add-modules javafx.base,javafx.graphics \
--add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED\
-XX:TieredStopAtLevel=1 -noverify \
-Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote \
-Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain \
-Dspring.application.admin.enabled=true \
"-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=58874:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\bin" \
-Dfile.encoding=UTF-8 \
-p C:\Users\xxx\.m2\repository\org\openjfx\javafx-base\12.0.2\javafx-base-12.0.2-win.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\12.0.2\javafx-graphics-12.0.2-win.jar;\
C:\Users\xxx\IdeaProjects\demo\target\classes;\
C:\Users\xxx\.m2\repository\org\springframework\boot\spring-boot-starter\2.1.7.RELEASE\spring-boot-starter-2.1.7.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\boot\spring-boot\2.1.7.RELEASE\spring-boot-2.1.7.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.7.RELEASE\spring-boot-autoconfigure-2.1.7.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.1.7.RELEASE\spring-boot-starter-logging-2.1.7.RELEASE.jar;\
C:\Users\xxx\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\xxx\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;\
C:\Users\xxx\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;\
C:\Users\xxx\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;\
C:\Users\xxx\.m2\repository\org\slf4j\jul-to-slf4j\1.7.26\jul-to-slf4j-1.7.26.jar;\
C:\Users\xxx\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;\
C:\Users\xxx\.m2\repository\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;\
C:\Users\xxx\.m2\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;\
C:\Users\xxx\.m2\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-base\12.0.2\javafx-base-12.0.2.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\12.0.2\javafx-graphics-12.0.2.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-controls\12.0.2\javafx-controls-12.0.2.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-controls\12.0.2\javafx-controls-12.0.2-win.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-fxml\12.0.2\javafx-fxml-12.0.2.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-fxml\12.0.2\javafx-fxml-12.0.2-win.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-swing\12.0.2\javafx-swing-12.0.2.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-swing\12.0.2\javafx-swing-12.0.2-win.jar\
-m com.javafxswingexplore.demo/com.javafxswingexplore.demo.DemoApplication

这是我收到的错误:

Exception in thread "JavaFX Application Thread" java.lang.UnsatisfiedLinkError: com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN.overrideNativeWindowHandle(Ljava/lang/Class;Ljdk/swing/interop/LightweightFrameWrapper;JLjava/lang/Runnable;)V
at javafx.swing/com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN.overrideNativeWindowHandle(Native Method)
at javafx.swing/com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN.overrideNativeWindowHandle(SwingNodeInteropN.java:128)
at javafx.swing/javafx.embed.swing.SwingNode.notifyNativeHandle(SwingNode.java:320)
at javafx.swing/javafx.embed.swing.SwingNode.addSceneListeners(SwingNode.java:598)
at javafx.swing/javafx.embed.swing.SwingNode.doCreatePeer(SwingNode.java:636)
at javafx.swing/javafx.embed.swing.SwingNode$2.doCreatePeer(SwingNode.java:137)
at javafx.swing/com.sun.javafx.embed.swing.SwingNodeHelper.createPeerImpl(SwingNodeHelper.java:65)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.createPeer(NodeHelper.java:94)
at javafx.graphics/javafx.scene.Node.getPeer(Node.java:2600)
at javafx.graphics/javafx.scene.Parent.doUpdatePeer(Parent.java:177)
at javafx.graphics/javafx.scene.Parent$1.doUpdatePeer(Parent.java:109)
at javafx.graphics/com.sun.javafx.scene.ParentHelper.updatePeerImpl(ParentHelper.java:78)
at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.updatePeerImpl(RegionHelper.java:72)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.updatePeer(NodeHelper.java:102)
at javafx.graphics/javafx.scene.Node.syncPeer(Node.java:710)
at javafx.graphics/javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2380)
at javafx.graphics/javafx.scene.Scene$ScenePulseListener.synchronizeSceneNodes(Scene.java:2356)
at javafx.graphics/javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2512)
at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:412)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:411)
at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:438)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:562)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:542)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:535)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:341)
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$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:835)

编辑:错误消息

设置缓存位置和 javafx.verbose true 后-Djavafx.cachedir=C:\\Users\\xxx\\Temp\\cache -Djavafx.verbose=true:

"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" -Djavafx.cachedir=C:\\Users\\xxx\\Temp\\cache -Djavafx.verbose=true --add-modules javafx.base,javafx.graphics ... -m 
com.javafxswingexplore.demo/com.javafxswingexplore.demo.DemoApplication

JavaFX launchApplication method: launchMode=LM_MODULE
WARNING: java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\bin\api-ms-win-core-console-l1-1-0.dll
...
WARNING: java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\bin\msvcp140.dll
Loaded C:\Program Files\Java\jdk-12.0.1\bin\msvcp140.dll from java.library.path
WARNING: java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\bin\concrt140.dll
Loaded C:\WINDOWS\system32\concrt140.dll from java.library.path
WARNING: java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\bin\prism_d3d.dll
Loaded C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\jbr\bin\prism_d3d.dll from java.library.path
JavaFX: using com.sun.javafx.tk.quantum.QuantumToolkit
WARNING: java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\bin\glass.dll
Loaded C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\jbr\bin\glass.dll from java.library.path
Calling main(String[]) method

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::

2019-08-16 17:49:48.830 INFO 29056 --- [JavaFX-Launcher] o.s.boot.SpringApplication : Starting application on DESKTOP-0DQOIUF with PID 29056 (started by xxx in C:\Users\xxx\IdeaProjects\demo)
2019-08-16 17:49:48.832 INFO 29056 --- [JavaFX-Launcher] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2019-08-16 17:49:49.217 INFO 29056 --- [JavaFX-Launcher] o.s.boot.SpringApplication : Started application in 0.692 seconds (JVM running for 1.973)
com.sun.javafx.fxml.ModuleHelper : <clinit>
getModuleMethod = public java.lang.Module java.lang.Class.getModule()
getResourceAsStreamMethod = public java.io.InputStream java.lang.Module.getResourceAsStream(java.lang.String) throws java.io.IOException
thisModule = module javafx.fxml
methodModule = module javafx.graphics
m = public javafx.collections.ObservableList javafx.scene.layout.Pane.getChildren()
WARNING: java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\bin\javafx_font.dll
Loaded C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\jbr\bin\javafx_font.dll from java.library.path
WARNING: java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\bin\prism_common.dll
Loaded C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\jbr\bin\prism_common.dll from java.library.path
Exception in thread "JavaFX Application Thread" java.lang.UnsatisfiedLinkError: com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN.overrideNativeWindowHandle(Ljava/lang/Class;Ljdk/swing/interop/LightweightFrameWrapper;JLjava/lang/Runnable;)V

最佳答案

即使您有 Maven 项目,您也是从 IntelliJ 运行(使用 Spring Boot 插件)。可以看到命令行:

"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" \
--add-modules javafx.base,javafx.graphics \
--add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED\
-XX:TieredStopAtLevel=1 -noverify \
-Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote \
-Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain \
-Dspring.application.admin.enabled=true \
"-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=58874:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\bin" \
-Dfile.encoding=UTF-8 \
-p C:\Users\xxx\.m2\repository\org\openjfx\javafx-base\12.0.2\javafx-base-12.0.2-win.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-graphics\12.0.2\javafx-graphics-12.0.2-win.jar;\
C:\Users\xxx\IdeaProjects\demo\target\classes;\
C:\Users\xxx\.m2\repository\org\springframework\boot\spring-boot-starter\2.1.7.RELEASE\spring-boot-starter-2.1.7.RELEASE.jar;\
...
C:\Users\xxx\.m2\repository\org\openjfx\javafx-controls\12.0.2\javafx-controls-12.0.2-win.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-fxml\12.0.2\javafx-fxml-12.0.2-win.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-swing\12.0.2\javafx-swing-12.0.2.jar;\
C:\Users\xxx\.m2\repository\org\openjfx\javafx-swing\12.0.2\javafx-swing-12.0.2-win.jar\
-m com.javafxswingexplore.demo/com.javafxswingexplore.demo.DemoApplication

尽管如此,所有模块都是从 pom 的依赖项解析的。 JavaFX 依赖项是从 Maven Central 下载的。

但是,运行该项目会抛出:

Exception in thread "JavaFX Application Thread" java.lang.UnsatisfiedLinkError: com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN.overrideNativeWindowHandle(Ljava/lang/Class;Ljdk/swing/interop/LightweightFrameWrapper;JLjava/lang/Runnable;)V

这意味着 native 方法 overrideNativeWindowHandle在 native 库中找不到,在本例中是 prism_common.dll,它与 javafx-graphics-12.0.2-win.jar 捆绑在一起。

正如评论中所讨论的,为了找出失败的原因,我们首先检查了默认存储 native 库的本地缓存:但是 C:\Users\xxx\.openjfx\cache\12.0.2 不存在。

尝试添加不同的缓存文件夹(使用javafx.cachedir)也不起作用。

但是添加 -Djavafx.verbose=true 可以通过在控制台日志中找出 native 库的来源来帮助解决:

Loaded C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\jbr\bin\prism_common.dll from java.library.path

IntelliJ 正在从 java.library.path 加载 prism_common.dll,在本例中设置为 JetBrains Runtime JBR,路径为 C:\Program文件\JetBrains\IntelliJ IDEA 2018.3.1\jbr\lib

可能是这个JBR (无论是 8 还是 11)与 OpenJFX 源不匹配,这就是我们收到上面列出的错误的原因。

解决方案

在这种情况下,解决方案非常简单:不依赖 JBR 库,而是通过设置不同的路径直接依赖 OpenJFX 依赖项提供的库。为此,只需转到“运行”->“编辑配置”,然后选择“Spring Boot”->“应用程序”,然后将此 VM 选项添加到环境中:

-Djava.library.path=C:\temp

保存并再次运行。现在它应该按预期工作。

关于java - 在 OpenJFX 中使用 SwingNode 出现不满意的链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57526916/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com