2

I have a JavaFX 8 desktop application and I'm creating an .app application bundle to distribute the application to Mac users. I use the Oracle “Self-Contained Application Packaging” tool to generate the bundle.

The problem that I have relates to the files associated with my application. I am associating the extension .wordy with these files. If I have the application open and I double click one of these files in the Mac Finder, my application receives an OpenFilesEvent containing the path to the file and everything works perfectly. If, however, the application is not open, double clicking a .wordy file in the Finder opens my application as I would expect but I never receive the event containing the path to the file that the user double-clicked on.

The file association is done in the Ant script for the Oracle “Self-Contained Application Packaging” tool, as follows:

<project name="VocabHunter Packaging" basedir=""
         xmlns:fx="javafx:com.sun.javafx.tools.ant">
    ...
    <fx:info title="VocabHunter">
        <fx:association description="VocabHunter session"
                        extension="wordy"
                        mimetype="application/x-vnd.VocabHunterSession"
                        icon="${basedir}/icons/mac/VocabHunterSession.icns"/>
    </fx:info>
    ...
</project>

In the Java code, I obtain an instance of com.apple.eawt.Application and then register the listener for the OpenFilesEvent as follows:

Application application = Application.getApplication();
application.setOpenFileHandler(new OsxOpenFilesHandler(listener));

You can see the full code here.

Does anyone know how to fix this so that I receive an event containing the path to the .wordy file even if the application was not running at the moment that the file was double clicked?

In the interests of completeness, I'm using the Oracle JDK 1.8.0_66 for Mac.

Adam
  • 682
  • 1
  • 6
  • 27

4 Answers4

1

I tested with your code and also met this problem. But when I used code directly in start(Stage primaryStage) method in to listen like this:

Application lowLevelApp = com.sun.glass.ui.Application.GetApplication();
lowLevelApp.setEventHandler {...}

I can get the OpenFilesEvent when first double clicked on file.

  • You're right, it does! I'm not sure how to make use of this to cleanly solve the problem though as it uses a com.sun... class and also seems to be very low level. It's an interesting clue though. – Adam Nov 28 '16 at 19:32
1

There is an entry in the bug database for this issue. https://bugs.openjdk.java.net/browse/JDK-8187992

mipa
  • 10,369
  • 2
  • 16
  • 35
0

You probably call the application.setOpenFileHandler() code too late during the application initialization. Try calling it as early as possible in main() and see if that solves the problem. I am not sure exactly when Mac OS X passes the OpenFile event to Java, but if at that time you haven't prepared by calling application.setOpenFileHandler() then the event will get lost.

Saeid Nourian
  • 1,606
  • 15
  • 32
  • Thanks for your suggestion @Saeid Nourian. Unfortunately, this doesn't seem to be the cause of the problem. I've tried double-checking by hacking the code and adding the line `Application.getApplication().setOpenFileHandler(e -> LOG.info("Received event {}", e));` as the very first line in `main()` (and removing the other listener registration for good measure). The listener still doesn't get called for a double-click on a `.wordy` file to open the application. As before, it is only called if the application was already open. – Adam Mar 30 '16 at 19:57
  • That's strange. I'm not sure what could be causing that but feel free to take a look at our working application here to compare with your code: https://github.com/concord-consortium/energy3d – Saeid Nourian Mar 31 '16 at 20:46
  • p.s. In my code I called Application.getApplication().setOpenFileHandler() after creating the JFrame gui but before making it visible. Not sure if that makes a difference. – Saeid Nourian Apr 01 '16 at 14:21
  • Thanks. I'll take a look at the code to see if there are any obvious differences. – Adam Apr 04 '16 at 07:38
  • btw which version of Mac OS X do you have? I remember in OS X 10.6 setOpenFileHandler didn't work for me reliably. Try this, download my application and see if file association works with my application in your OS X machine: http://www.runiter.com/graphing-calculator/download.htm – Saeid Nourian Apr 06 '16 at 01:34
  • I'm using OS X 10.11.4 and am now testing with the Oracle JDK 1.8.0_77. I tried with your Energy3D application (very nice, by the way!) and everything seems to work as it should regarding file associations. I've taken a look round in your code and the big difference I think is that I'm using JavaFX whereas Energy3D is Swing. Other than that, I'm still stumped. I've also tried moving around the logging hack, just in case this is related to the point that the listener is registered but to no avail. – Adam Apr 10 '16 at 10:27
0

For future reference: getting the openFileHandler to work correctly can be very tricky. The handler is invoked from an event handler on the UI event thread, which means there is no guarantee that main() has completed when the handler is run. For best results, the openFileHandler should be set up using a static initializer, and main() and the open file handler should both invoke the same initialization code on the UI event thread, and the initialization code should be written to work if called more than once.

Alan Snyder
  • 408
  • 2
  • 13