简体   繁体   中英

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. Up to now, this has worked as expected. However, lately (since 10.7? not sure as to the timeline), sometimes this does not work. 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.

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. 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. I also have not been able to find any way of controlling any aspects of event dispatch via a configuration in Info.plist.

Confirmed that this is a file open event coming from the 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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