3

From a different question here I found a code snippet to handle the open file event in my java app. However, even though I'm building my app in a Mac OS X environment and my IDE can find the com.aple.eawt.* classes (in rt.jar in the Oracle SDK I'm using), maven does not find them. I've looked for clues about what I need to do in pom.xml to hint to maven about the classpath, but really the advice seems to boil down to:

1) Add whatever jar you need into a project repo

2) or use bad form and link to the specific jar.

Since I'm trying to build this app in a way that it will still run on other platforms, I'd like to solve this without having to bundle the whole Apple-specific runtime into the build.

Stephen B.
  • 396
  • 2
  • 19
  • So you want your app to be compiled on multiple platform, right? And then you want only it to be executed only on a Mac? – Tunaki Feb 22 '16 at 21:21
  • Not quite. It should compile on a Mac, or on a Linux machine. The app should run on a Mac, on a Linux machine, or on a Windows machine. The code that is Mac-specific is inside a block defended by, "if this is a Mac, add this handler," so the other platforms won't try to do it. – Stephen B. Feb 22 '16 at 21:23
  • 1
    Okay. I've never tried it but I think you just need to add [this dependency](http://ymasory.github.io/OrangeExtensions/) to your POM. – Tunaki Feb 22 '16 at 21:27

1 Answers1

3

Thanks to Tunaki's link above, the answer is really that simple (I'd tried adding the Apple jar to my pom and that didn't work).

<dependency>
  <groupId>com.yuvimasory</groupId>
  <artifactId>orange-extensions</artifactId>
  <version>1.3.0</version>
</dependency>

Further, in my main class, my code had to look a little different, since the orange-extension version of OpenFilesEvent.getFiles() returns a collection of Object rather than of File:

  if (System.getProperty("os.name").contains("OS X")){
    com.apple.eawt.Application a = com.apple.eawt.Application.getApplication();
    a.setOpenFileHandler(new com.apple.eawt.OpenFilesHandler() {

        @Override
        public void openFiles(com.apple.eawt.AppEvent.OpenFilesEvent e) {
            for (Object oFile : e.getFiles()){
                if (oFile instanceof File) {
                    File file = (File) oFile;
                    // do the actual open logic here
                } else {
                    __l.warn("The OS just told us to open a file but handed us "+oFile.toString());
                }
            }
        }

    });
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Stephen B.
  • 396
  • 2
  • 19