You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX中FileChooser调用showOpenDialog()输出错误如何屏蔽?

JavaFX调用FileChooser.showOpenDialog()出现「+[CATransaction synchronize] called within transaction」日志的问题解析

一、日志含义

这是macOS Core Animation框架输出的警告日志,提示代码在一个已存在的CATransaction事务内部,再次调用了CATransaction synchronize方法,属于框架层面的事务嵌套冲突,但不会影响功能正常运行。

二、出现原因

JavaFX在macOS平台上实现FileChooser时,底层调用了原生Cocoa文件选择对话框。JavaFX的UI线程事务与macOS Core Animation的CATransaction发生嵌套冲突,导致Core Animation抛出该警告。该问题存在于JavaFX 11至19版本,属于JavaFX对macOS原生组件适配的小瑕疵,不影响业务功能执行。

三、屏蔽方法

由于这是macOS原生框架输出的日志,需通过JVM参数或系统日志配置屏蔽:

方法1:添加JVM启动参数

启动Java程序时,添加以下参数抑制这类日志输出:

-Djava.util.logging.config.file=logging.properties

同时创建logging.properties文件,写入内容:

.level=INFO
com.sun.javafx.level=INFO
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

或者直接通过JVM参数简化配置:

-Dapple.awt.UIElement=true -Djava.util.logging.config.file=logging.properties

方法2:修改macOS系统日志配置

  • 临时屏蔽(重启系统后失效):在终端执行
sudo log config --mode "level:default" --subsystem com.apple.CoreAnimation
  • 永久生效:创建/Library/Preferences/com.apple.CoreAnimation.plist文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>LoggingLevel</key>
    <string>Default</string>
</dict>
</plist>

再执行命令加载配置:

sudo defaults write /Library/Preferences/com.apple.CoreAnimation.plist LoggingLevel -string Default

代码示例

import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class HelloApplication extends Application {
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage stage) throws IOException {
        FileChooser chooser = new FileChooser();
        File file = chooser.showOpenDialog(stage);
    }
}

控制台输出日志

2022-12-27 20:41:49.955 java[75677:2827731] +[CATransaction synchronize] called within transaction
2022-12-27 20:41:50.093 java[75677:2827731] +[CATransaction synchronize] called within transaction

内容的提问来源于stack exchange,提问作者Ahmad Mtera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:05:20