4

Java 9 prevents direct use of com.apple.eio.FileManager. Until now I've used this on Mac to get the location of the user's "Desktop" path, as follows

public static File getDesktopFolder() throws FileNotFoundException {
    final int desktopOSType = FileManager.OSTypeToInt("desk");
    final String desktopFolderName = FileManager.findFolder(desktopOSType);
    return new File(desktopFolderName);
}

Is there a replacement in Java 9 for using com.apple.eio.FileManager to find the Desktop? My temporary solution is to use the answer to the similar Windows-oriented question here:

public static File getDesktopFolder() {
    return new File(System.getProperty("user.home"), "Desktop");
}

However this seems brittle and potentially buggy in certain locales or on certain strangely-configured systems.

Community
  • 1
  • 1
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184

1 Answers1

3

Platform-Specific Desktop Features have been removed in JDK9.

The APIs in the com.apple.eawt and com.apple.eio packages are encapsulated, so you won’t be able to compile against them in JDK 9. However, they remain accessible at runtime, so existing code that is compiled to old versions continues to run.

Eventually, libraries or applications that use the internal classes in the appleand com.apple packages and their subpackages will need to migrate to the new API.

The new API to migrate the code instead as proposed in the JEP:272 is java.awt.Desktop. Though I can see edit, open, browse etc stubs within the same API, yet all of them need a legal specified path name and none of them creates one according to the docs.

Also, I am not very sure of why you would find the existing approach as brittle and buggy for the purpose of creating a file on a user's desktop.

return new File(System.getProperty("user.home"), "Desktop");

An alternate way/temporary hack to make use of your existing code in Java 9, when you might end up getting an error similar to

Error:(3, 17) java: package com.apple.eio is not visible   (package
com.apple.eio is declared in module java.desktop, which does not
export it)

is to make use of --add-exports as proposed in Module System during compilation

--add-exports java.desktop/com.apple.eio=ALL-UNNAMED
Naman
  • 27,789
  • 26
  • 218
  • 353