简体   繁体   English

Mac OS X上的Java应用程序并不总是从OS接收打开文件事件

[英]Java application on Mac OS X does not always receive open file events from the OS

My java application's main class has a static initializer where we set up the ApplicationAdapter to listen for notifications from the OS, before main() is invoked. 我的Java应用程序的主类有一个静态初始化程序,我们在调用main()之前将ApplicationAdapter设置为侦听来自OS的通知。 Up to now, this has worked as expected. 到目前为止,这已经按预期进行。 However, lately (since 10.7? not sure as to the timeline), sometimes this does not work. 但是,最近(从10.7开始?不确定时间轴),有时这是行不通的。 After debugging the app, it seems as though the OS is posting the file open event asynchronously on the event thread while the static initializer is still running. 调试应用程序后, 似乎操作系统在静态初始化程序仍在运行的同时在事件线程上异步发布了文件打开事件。 So sometimes the initialization completes before the file open event is posted (and the file is opened correctly) and other times, the event is posted before initialization is complete (ie before my app has a chance to register an ApplicationListener) and so my app never gets the chance to process the event, and the requested document is not opened. 因此,有时初始化在发布文件打开事件(并且文件已正确打开)之前完成,而有时在初始化完成之前(即在我的应用程序有机会注册ApplicationListener之前)发布事件。有机会处理事件,并且未打开请求的文档。

Has anyone else come across this problem? 还有其他人遇到这个问题吗?

One possible solution I've tried is to pause the event queue as at the beginning of the main class's static initializer, something like: 我尝试过的一种可能的解决方案是在主类的静态初始化程序的开头暂停事件队列,例如:

static{
    final Object monitor = new Object();
    SwingUtilities.invokeLater( new Runnable(){
       public void run(){
            synchronized( monitor) {
                try{ monitor.wait(); } catch( Exception e ){ e.printStackTrace(); }
            }
       }
    });

    registerApplicationListener();

    synchronized( monitor) {
        try{ monitor.notifyAll(); }
        catch(Exception e){ e.printStackTrace(); }
    }
}

This blocks event dispatch while the application initialization sets up the ApplicationListener to receive OS events. 当应用程序初始化将ApplicationListener设置为接收OS事件时,这将阻止事件分发。 However, the problem remains that the OS could still dispatch the events before the initialization is complete, and there's nothing I can do about that, as far as I can tell. 但是,问题仍然在于,操作系统仍然可以在初始化完成之前调度事件,据我所知,我对此无能为力。 Nothing in the Apple Java extensions API indicates any way to control event dispatch behaviour. Apple Java扩展API中没有任何内容指示控制事件分发行为的任何方式。 I also have not been able to find any way of controlling any aspects of event dispatch via a configuration in Info.plist. 我还无法通过Info.plist中的配置找到任何方法来控制事件分发的任何方面。

Confirmed that this is a file open event coming from the OS X shell. 确认这是来自OS X Shell的文件打开事件。 Java applications cant catch this notification reliably and a workaround is to install a launch daemon to catch the notification and forward to the application as startup params. Java应用程序无法可靠地捕获此通知,一种解决方法是安装启动守护程序以捕获该通知并作为启动参数转发给应用程序。

Registering a URL scheme for the app might also work but Ive never tried it with a Java app hopefully the params would come to the main() args 为该应用程序注册URL方案也可能有效,但我从未尝试过与Java应用程序一起使用,希望参数会出现在main()参数中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM